明确表关系
用户和角色之间的关系是多对多。
多对多的表关系建立靠的是中间表,其中用户表和中间表的关系是一对多,角色表和中间表的关系也是一对多,如下图所示:
实体类关系建立以及映射配置
1、创建一个不使用maven的模块
2、引入pom坐标
<properties><spring.version>4.2.4.RELEASE</spring.version><hibernate.version>5.0.7.Final</hibernate.version><slf4j.version>1.6.6</slf4j.version><log4j.version>1.2.12</log4j.version><c3p0.version>0.9.1.2</c3p0.version><mysql.version>8.0.15</mysql.version></properties><dependencies><!-- junit单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version><scope>test</scope></dependency><!-- spring beg --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.6.8</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><!-- spring end --><!-- hibernate beg --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${hibernate.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>${hibernate.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.2.1.Final</version></dependency><!-- hibernate end --><!-- c3p0 beg --><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>${c3p0.version}</version></dependency><!-- c3p0 end --><!-- log end --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- log end --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>1.9.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- el beg 使用spring data jpa 必须引入 --><dependency><groupId>javax.el</groupId><artifactId>javax.el-api</artifactId><version>2.2.4</version></dependency><dependency><groupId>org.glassfish.web</groupId><artifactId>javax.el</artifactId><version>2.2.4</version></dependency><!-- el end --></dependencies>
3、创建实体类,并建立表关系
- 一个用户可以具有多个角色,所以在用户实体类中应该包含多个角色的信息,代码如下:
/*** 用户的数据模型*/
@Entity
@Table(name="sys_user")
public class SysUser implements Serializable {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="user_id")private Long userId;@Column(name="user_code")private String userCode;@Column(name="user_name")private String userName;@Column(name="user_password")private String userPassword;@Column(name="user_state")private String userState;//多对多关系映射@ManyToMany(mappedBy="users")private Set<SysRole> roles = new HashSet<SysRole>(0);public Long getUserId() {return userId;}public void setUserId(Long userId) {this.userId = userId;}public String getUserCode() {return userCode;}public void setUserCode(String userCode) {this.userCode = userCode;}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 String getUserState() {return userState;}public void setUserState(String userState) {this.userState = userState;}public Set<SysRole> getRoles() {return roles;}public void setRoles(Set<SysRole> roles) {this.roles = roles;}@Overridepublic String toString() {return "SysUser [userId=" + userId + ", userCode=" + userCode + ", userName=" + userName + ", userPassword="+ userPassword + ", userState=" + userState + "]";}
}
- 一个角色可以赋予多个用户,所以在角色实体类中应该包含多个用户的信息,代码如下:
/*** 角色的数据模型*/
@Entity
@Table(name="sys_role")
public class SysRole implements Serializable {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="role_id")private Long roleId;@Column(name="role_name")private String roleName;@Column(name="role_memo")private String roleMemo;//多对多关系映射@ManyToMany@JoinTable(name="user_role_rel",//中间表的名称//中间表user_role_rel字段关联sys_role表的主键字段role_idjoinColumns={@JoinColumn(name="role_id",referencedColumnName="role_id")},//中间表user_role_rel的字段关联sys_user表的主键user_idinverseJoinColumns={@JoinColumn(name="user_id",referencedColumnName="user_id")})private Set<SysUser> users = new HashSet<SysUser>(0);public Long getRoleId() {return roleId;}public void setRoleId(Long roleId) {this.roleId = roleId;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public String getRoleMemo() {return roleMemo;}public void setRoleMemo(String roleMemo) {this.roleMemo = roleMemo;}public Set<SysUser> getUsers() {return users;}public void setUsers(Set<SysUser> users) {this.users = users;}@Overridepublic String toString() {return "SysRole [roleId=" + roleId + ", roleName=" + roleName + ", roleMemo=" + roleMemo + "]";}
}
映射的注解说明
@ManyToMany
- 作用:用于映射多对多关系
- 属性:
- cascade:配置级联操作。
- fetch:配置是否采用延迟加载。
- targetEntity:配置目标的实体类。映射多对多的时候不用写。
@JoinTable
- 作用:针对中间表的配置
- 属性:
- name:配置中间表的名称
- joinColumns:中间表的外键字段关联当前实体类所对应表的主键字段
- inverseJoinColumn:中间表的外键字段关联对方表的主键字段
@JoinColumn
- 作用:用于定义主键字段和外键字段的对应关系。
- 属性:
- name:指定外键字段的名称
- referencedColumnName:指定引用主表的主键字段名称
- unique:是否唯一。默认值不唯一
- nullable:是否允许为空。默认值允许。
- insertable:是否允许插入。默认值允许。
- updatable:是否允许更新。默认值允许。
- columnDefinition:列的定义信息。