用户-角色-权限hibernate经典配置。
既然有人问起,我就写下说明吧。在文章中间的配置文件那里。权当回忆一下,也帮助更多人。这是以前学校时写的,没有注释。都是贴的代码笔记。看到的莫要见怪。欢迎学习交流。
package com.tudou.hibernates.RoleManage;
import java.util.HashSet;
import java.util.Set;
public class User {
private int uid;
private String userName;
private String nickName;
private String password;
private String mark;
private Set roleUsers = new HashSet();
public User() {
super();
}
public User(String userName, String nickName, String password, String mark,
Set roleUsers) {
super();
this.userName = userName;
this.nickName = nickName;
this.password = password;
this.mark = mark;
this.roleUsers = roleUsers;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public Set getRoleUsers() {
return roleUsers;
}
public void setRoleUsers(Set roleUsers) {
this.roleUsers = roleUsers;
}
}
package com.tudou.hibernates.RoleManage;
import java.util.HashSet;
import java.util.Set;
public class Role {
private int rid;
private String roleName;
private String dsc;
private Set roleUsers = new HashSet();
private Set functionRoles = new HashSet();
public Role() {
super();
}
public Role(String roleName, String dsc, Set roleUsers,
Set functionRoles) {
super();
this.roleName = roleName;
this.dsc = dsc;
this.roleUsers = roleUsers;
this.functionRoles = functionRoles;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getDsc() {
return dsc;
}
public void setDsc(String dsc) {
this.dsc = dsc;
}
public Set getRoleUsers() {
return roleUsers;
}
public void setRoleUsers(Set roleUsers) {
this.roleUsers = roleUsers;
}
public Set getFunctionRoles() {
return functionRoles;
}
public void setFunctionRoles(Set functionRoles) {
this.functionRoles = functionRoles;
}
}
package com.tudou.hibernates.RoleManage;
import java.util.HashSet;
import java.util.Set;
public class Functions {
private int fid;
private String functionName;
private String mark;
private Set functionRoles = new HashSet();
public Functions(String functionName, String mark,
Set functionRoles) {
super();
this.functionName = functionName;
this.mark = mark;
this.functionRoles = functionRoles;
}
public Functions() {
super();
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public Set getFunctionRoles() {
return functionRoles;
}
public void setFunctionRoles(Set functionRoles) {
this.functionRoles = functionRoles;
}
}
用户-角色-权限关系如下:
一个用户user对应多个角色role,一个角色对应多个用户,关系为多对多many-to-many
一个角色role对应多个权限functions,一个权限也对应多个角色。
这个自不必说。
主要是如何维护呢。
如下所示:
多对多必然产生中间表进行关联。
user与role产生中间表userRole。则user与userRole的关系为one-to-many。理解为一个用户可以有多个权限对应。role与userRole的关系为one-to-many。理解为一个权限可以有多个用户对应。
其次就是角色role与权限function,中间表为roleFunction,他们之前的关系如上。
只不过role表在中间,即与user表是多对多的关系,又与function表是多对多的关系。所以role会产生2张中间表,一张userRole,一张roleFunction而已。
role与这2张中间表都是one-to-many的关系,反之,它们与role却都是many-to-one的关系。
user->userRole->role->roleFunction->function
user->userRole一对多,role->userRole一对多,则user->role多对多
role->roleFunction一对多,function->roleFunction一对多,则role->function多对多
说白了就是把2个多对多,分解成了4个一对多【其实就是2对一对多】而已。
这样写来。再看配置,则是一目了然了。
package com.tudou.hibernates.RoleManage;
public class RoleUser {
private int ru_id;
private Role role;
private User user;
public RoleUser(Role role, User user) {
super();
this.role = role;
this.user = user;
}
public RoleUser() {
super();
}
public int getRu_id() {
return ru_id;
}
public void setRu_id(int ruId) {
ru_id = ruId;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
package com.tudou.hibernates.RoleManage;
public class FunctionsRole {
private int fr_id;
private Functions functions;
private Role role;
public FunctionsRole() {
super();
}
public FunctionsRole(Functions functions, Role role) {
super();
this.functions = functions;
this.role = role;
}
public int getFr_id() {
return fr_id;
}
public void setFr_id(int frId) {
fr_id = frId;
}
public Functions getFunctions() {
return functions;
}
public void setFunctions(Functions functions) {
this.functions = functions;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}