用的 spring mvc + mybatis3 框架,假设有 用户与角色两张表, 以及一张关联表,分别对应的实体 user
、role
和 user_role
. 在 service 层, 为这 2 个对象分别建立 IUserService
, IRoleService
。
我想问的是:
user_role
吗?user_role
, 需要建IUserRoleService
吗?多谢指点!
高洛峰2017-04-17 13:03:24
No need at all. The database design does not have to be consistent with the Java object design. Just include role in your user object, or the role object contains user.
Supplement:
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private UserRoleRepository userRoleRepository;
public void deleteUser(User user) {
userRepository.delete(user);
userRoleRepository.deleteRoles(user);
}
}
public class RoleService {
@Autowired
private RoleRepository roleRepository;
@Autowired
private UserRoleRepository userRoleRepository;
public void deleteRole(Role role) {
roleRepository.delete(role);
userRoleRepository.deleteUsers(role);
}
}
PHP中文网2017-04-17 13:03:24
I would like to ask again, to obtain the role corresponding to the user, should it be placed in getUserRoles(User user)
, UserService
or RoleService
? What is the basis for judgment?
迷茫2017-04-17 13:03:24
It seems that it can be placed anywhere. If you want to avoid arguments, put it in a separate command