项目中处理用户与角色的关系

用户:用户1,用户2
角色:管理员,一般用户

用户与角色的关系:多对多
一个用户可以有多个角色;一个角色可以被多个用户使用

用户:user
用户id,名称...
1      用户1
2      用户2

用户角色:user_role
用户id,角色id
1       1
1       2
2       2


角色:role
角色Id,名称
1      管理员
2      一般用户


查询出用户后;需要知道这个用户有哪些角色


1.User

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
	
	private String id;//用户ID
	private String dept;//部门
	private String account;//账号
	private String name;//用户名
	private String password;//密码
	
	private String headImg;//头像
	private boolean gender;//性别
	private String state;//状态
	private String mobile;//手机号
	private String email;//电子邮箱
	private Date birthday;//生日
	private String memo;//描述
	
	private List userRoles;//用户角色
	
	//用户状态
	public static String USER_STATE_VALID = "1";//有效
	public static String USER_STATE_INVALID = "0";//无效
	
	public User() {
	}
	public User(String id, String dept, String account, String name, String password, String headImg, boolean gender, String state, String mobile, String email, Date birthday, String memo) {
		this.id = id;
		this.dept = dept;
		this.account = account;
		this.name = name;
		this.password = password;
		this.headImg = headImg;
		this.gender = gender;
		this.state = state;
		this.mobile = mobile;
		this.email = email;
		this.birthday = birthday;
		this.memo = memo;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHeadImg() {
		return headImg;
	}
	public void setHeadImg(String headImg) {
		this.headImg = headImg;
	}
	public boolean isGender() {
		return gender;
	}
	public void setGender(boolean gender) {
		this.gender = gender;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	public List getUserRoles() {
		return userRoles;
	}
	public void setUserRoles(List userRoles) {
		this.userRoles = userRoles;
	}
}

2.UserRole

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;

public class UserRole implements Serializable {

	private UserRoleId id;

	public UserRole() {
	}

	public UserRole(UserRoleId id) {
		this.id = id;
	}

	public UserRoleId getId() {
		return id;
	}

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

3.UserRoleId

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;

import cn.itcast.nsfw.role.entity.Role;

public class UserRoleId implements Serializable {

	private Role role;
	private String userId;
	
	public UserRoleId() {
	}
	public UserRoleId(Role role, String userId) {
		this.role = role;
		this.userId = userId;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((role == null) ? 0 : role.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserRoleId other = (UserRoleId) obj;
		if (role == null) {
			if (other.role != null)
				return false;
		} else if (!role.equals(other.role))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}
	
}

4.User.hbm.xml





	
		
			
			
		
		
			
		
		
			
				
		
			
		
		
			
		
		
			
		
		
			
		
		
			
		
		
			
		
		
			
		
		
			
		
		
			
		
	


	

5.UserRole.hbm.xml





	
		
			
				
			
			
				
			
		
	


	



你可能感兴趣的:(ssh)