权限表设计之代码解析

在权限表设计中已经说了权限表的结构,在这里我说下代码

user表

</pre><pre name="code" class="html">@Entity
@Table(name="user")
public class User implements Serializable{

	
    private static final long serialVersionUID = 6177417450707400228L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="userid",nullable=false)
	private int userId;
    
	@Column(name="username")
	private String userName;
	
	@Column(name="userpassword")
    private String userPassword;
	
    /*
     * cascade:为级联操作,里面有级联保存,级联删除等,all为全部 
     * fetch:载入类型,有lazy和eager二种,
     *   eager为急载入,意为马上载入,在类载入时就载入,lazy为慢载入,第一次调用的时候再载入,因为数据量太大,onetomany一般为lazy
     * mappedBy:这个为manytoone中的对象名,这个不要变哦
     * 
     * <UserRole>:为要获得的mappedBy的类
     */
    @OneToMany(fetch=FetchType.LAZY,mappedBy="user",cascade=CascadeType.ALL)
	private List<UserRole> listUserRole; 
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public List<UserRole> getListUserRole() {
		return listUserRole;
	}

	public void setListUserRole(List<UserRole> listUserRole) {
		this.listUserRole = listUserRole;
	}	
} 

userrole表

@Entity
@Table(name="userrole")
public class UserRole implements Serializable{
    
	private static final long serialVersionUID = 6177417450707400228L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id",nullable=false)
	private int id;
	
	@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
	@JoinColumn(name="userid")
	private User user;
	
    @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
	@JoinColumn(name="roleid")
	private Role role;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	
}
role表

@Entity
@Table(name="role")
public class Role implements Serializable{
	
	
	private static final long serialVersionUID = 6177417450707400228L;   
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="roleid",nullable=false)
	private int roleId;
	
	@Column(name="rolename")
    private String roleName;
	
	@OneToMany(fetch=FetchType.LAZY,mappedBy="role",cascade=CascadeType.ALL)
	private List<UserRole> listUserRole;
	
	@OneToMany(fetch=FetchType.LAZY,mappedBy="role",cascade=CascadeType.ALL)
	private List<RolePermission> listRolePermission;
			
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
	public List<UserRole> getListUserRole() {
		return listUserRole;
	}
	public void setListUserRole(List<UserRole> listUserRole) {
		this.listUserRole = listUserRole;
	}
	
	public List<RolePermission> getListRolePermission() {
		return listRolePermission;
	}
	public void setListRolePermission(List<RolePermission> listRolePermission) {
		this.listRolePermission = listRolePermission;
	}	
}

rolepermission表

@Entity
@Table(name="rolepermission")
public class RolePermission implements Serializable{
    
	private static final long serialVersionUID = 6177417450707400228L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id",nullable=false)
	private int id;
	
	@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
	@JoinColumn(name="roleid")
	private Role role;

	@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
	@JoinColumn(name="permissionid")
	private Permission permission;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}
	
}

permission表

@Entity
@Table(name="permission")
public class Permission implements Serializable{
    
    private static final long serialVersionUID = 6177417450707400228L;
    
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="permissionid",nullable=false)
	private int permissionId;
	
    @Column(name="permission")
	private String permission;
    
	@OneToMany(fetch=FetchType.LAZY,mappedBy="permission",cascade=CascadeType.ALL)
	private List<RolePermission> listRolePermission;
    	
	public int getPermissionId() {
		return permissionId;
	}

	public void setPermissionId(int permissionId) {
		this.permissionId = permissionId;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public List<RolePermission> getListRolePermission() {
		return listRolePermission;
	}

	public void setListRolePermission(List<RolePermission> listRolePermission) {
		this.listRolePermission = listRolePermission;
	}
}

我这里是依照我的权限表博客里设计的,另外使用的onetomany和manytoone,在这里也能够用manytomany,

依据大家的喜好来写吧。

posted @ 2015-02-21 12:46  mengfanrong  阅读(376)  评论(0编辑  收藏  举报