通常狀況下使用三張表,用戶表user、角色表role、權限表permissionjava
表結構及關係以下圖app
Hibernate對應的pojo以下this
usercode
package com.shiro.pojo; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="t_user") public class User { private Integer id; private String username; private String password; private List<Role> roleList;//一個用戶具備多個角色 @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @ManyToMany @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="user_id")},inverseJoinColumns={@JoinColumn(name="role_id")}) public List<Role> getRoleList() { return roleList; } public void setRoleList(List<Role> roleList) { this.roleList = roleList; } @Transient public Set<String> getRolesName(){ List<Role> roles=getRoleList(); Set<String> set=new HashSet<String>(); for (Role role : roles) { set.add(role.getRolename()); } return set; } }
roleget
package com.shiro.pojo; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="t_role") public class Role { private Integer id; private String rolename; private List<Permission> permissionList;//一個角色對應多個權限 private List<User> userList;//一個角色對應多個用戶 @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRolename() { return rolename; } public void setRolename(String rolename) { this.rolename = rolename; } @OneToMany(mappedBy="role") public List<Permission> getPermissionList() { return permissionList; } public void setPermissionList(List<Permission> permissionList) { this.permissionList = permissionList; } @ManyToMany @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="role_id")},inverseJoinColumns={@JoinColumn(name="user_id")}) public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } @Transient public List<String> getPermissionsName(){ List<String> list=new ArrayList<String>(); List<Permission> perlist=getPermissionList(); for (Permission per : perlist) { list.add(per.getPermissionname()); } return list; } }
permissionit
package com.shiro.pojo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_permission") public class Permission { private Integer id; private String permissionname; private Role role;//一個權限對應一個角色 @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPermissionname() { return permissionname; } public void setPermissionname(String permissionname) { this.permissionname = permissionname; } @ManyToOne @JoinColumn(name="role_id") public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } }
user與role是多對多關係 產生t_user_role爲中間表io