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

用户:用户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<UserRole> 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<UserRole> getUserRoles() {
		return userRoles;
	}
	public void setUserRoles(List<UserRole> 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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="cn.itcast.nsfw.user.entity.User" table="user">
		<id name="id" type="java.lang.String">
			<column name="id" length="32" />
			<generator class="uuid.hex" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="20" not-null="true" />
		</property>
		<property name="dept" type="java.lang.String">
			<column name="dept" length="20" not-null="true" />
		</property>		
		<property name="account" type="java.lang.String">
			<column name="account" length="50" not-null="true" />
		</property>
		<property name="password" type="java.lang.String">
			<column name="password" length="50" not-null="true" />
		</property>
		<property name="headImg" type="java.lang.String">
			<column name="headImg" length="100" />
		</property>
		<property name="gender" type="java.lang.Boolean">
			<column name="gender" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" length="50" />
		</property>
		<property name="mobile" type="java.lang.String">
			<column name="mobile" length="20" />
		</property>
		<property name="birthday" type="java.util.Date">
			<column name="birthday" length="10" />
		</property>
		<property name="state" type="java.lang.String">
			<column name="state" length="1" />
		</property>
		<property name="memo" type="java.lang.String">
			<column name="memo" length="200" />
		</property>
	</class>

</hibernate-mapping>
	

5.UserRole.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="cn.itcast.nsfw.user.entity.UserRole" table="user_role">
		<composite-id name="id" class="cn.itcast.nsfw.user.entity.UserRoleId">
			<key-many-to-one name="role" lazy="false" class="cn.itcast.nsfw.role.entity.Role">
				<column name="role_id"></column>
			</key-many-to-one>
			<key-property name="userId" type="java.lang.String">
				<column name="user_id" length="32"></column>
			</key-property>
		</composite-id>
	</class>

</hibernate-mapping>
	



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值