t3用户-角色-权限hibernate经典配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了t3用户-角色-权限hibernate经典配置相关的知识,希望对你有一定的参考价值。
- 用户-角色-权限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<RoleUser> roleUsers = new HashSet<RoleUser>();
- public User() {
- super();
- }
- public User(String userName, String nickName, String password, String mark,
- Set<RoleUser> 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<RoleUser> getRoleUsers() {
- return roleUsers;
- }
- public void setRoleUsers(Set<RoleUser> 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<RoleUser> roleUsers = new HashSet<RoleUser>();
- private Set<FunctionsRole> functionRoles = new HashSet<FunctionsRole>();
- public Role() {
- super();
- }
- public Role(String roleName, String dsc, Set<RoleUser> roleUsers,
- Set<FunctionsRole> 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<RoleUser> getRoleUsers() {
- return roleUsers;
- }
- public void setRoleUsers(Set<RoleUser> roleUsers) {
- this.roleUsers = roleUsers;
- }
- public Set<FunctionsRole> getFunctionRoles() {
- return functionRoles;
- }
- public void setFunctionRoles(Set<FunctionsRole> 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<FunctionsRole> functionRoles = new HashSet<FunctionsRole>();
- public Functions(String functionName, String mark,
- Set<FunctionsRole> 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<FunctionsRole> getFunctionRoles() {
- return functionRoles;
- }
- public void setFunctionRoles(Set<FunctionsRole> 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对一对多】而已。
这样写来。再看配置,则是一目了然了。
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tudou.hibernates.RoleManage">
- <class name="User" table="tb_user">
- <id name="uid" column="uid">
- <generator class="native" />
- </id>
- <property name="userName"></property>
- <property name="nickName"></property>
- <property name="password"></property>
- <property name="mark"></property>
- <set name="roleUsers" cascade="save-update" inverse="true">
- <key>
- <column name="uid"></column>
- </key>
- <one-to-many class="RoleUser" />
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tudou.hibernates.RoleManage">
- <class name="Role" table="tb_role">
- <id name="rid" column="rid">
- <generator class="native">
- </generator>
- </id>
- <property name="roleName" />
- <property name="dsc" />
- <set name="roleUsers" cascade="save-update" inverse="true">
- <key>
- <column name="rid"></column>
- </key>
- <one-to-many class="RoleUser" />
- </set>
- <set name="functionRoles" cascade="save-update" inverse="true">
- <key>
- <column name="rid"></column>
- </key>
- <one-to-many class="FunctionsRole" />
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tudou.hibernates.RoleManage">
- <class name="RoleUser" table="role_user">
- <id name="ru_id" column="ru_id">
- <generator class="native" />
- </id>
- <many-to-one name="role" class="Role" cascade="save-update"
- fetch="select">
- <column name="rid"></column>
- </many-to-one>
- <many-to-one name="user" class="User" cascade="save-update"
- fetch="select">
- <column name="uid"></column>
- </many-to-one>
- </class>
- </hibernate-mapping>
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tudou.hibernates.RoleManage">
- <class name="FunctionsRole" table="functions_role">
- <id name="fr_id" column="fr_id">
- <generator class="native" />
- </id>
- <many-to-one name="functions" class="Functions" cascade="save-update"
- fetch="select">
- <column name="fid"></column>
- </many-to-one>
- <many-to-one name="role" class="Role" cascade="save-update"
- fetch="select">
- <column name="rid"></column>
- </many-to-one>
- </class>
- </hibernate-mapping>
- 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;
- }
- }
以上是关于t3用户-角色-权限hibernate经典配置的主要内容,如果未能解决你的问题,请参考以下文章