企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 一.`User.java` ***** ~~~ package net.youworker.domain; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-08 20:54 */ @Entity @Table(name = "sys_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id", columnDefinition = "bigint(20) unsigned") private Long UserId; //用户名 @Column(name = "user_name", nullable = false, columnDefinition = "varchar(30) default '' comment '用户名' ") private String userName = ""; //年龄 @Column(name = "user_age", nullable = false, columnDefinition = "tinyint(10) default 0 comment '年龄' ") private Integer userAge = 0; /** * 配置用户到角色的多对多关系 * <p> * 配置多对多的映射关系 * 1.声明表关系的配置 * 2.配置中间表 * * @JoinTable name: 中间表的名称 * joinColumns 配置当前对象在中间表的外键 * inverseJoinColumns 配置对方对象在中间表的外键 */ @ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL) @JoinTable( name = "sys_user_role", //joinColumns 当前对象在中间表的外键 joinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")}, //inverseJoinColumns 对方对象在中间表的外键 inverseJoinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")} ) private Set<Role> roles = new HashSet<>(); public Set<Role> getRoles() { return roles; } public User() { } public void setRoles(Set<Role> roles) { this.roles = roles; } public Long getUserId() { return UserId; } public void setUserId(Long userId) { UserId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getUserAge() { return userAge; } public void setUserAge(Integer userAge) { this.userAge = userAge; } } ***** ~~~ ### 二. `Role.java` ~~~ package net.youworker.domain; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-08 20:46 */ @Entity @Table(name = "sys_role") public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "role_id", columnDefinition = "bigint(20) unsigned") private Long roleId; //角色名称 @Column(name = "role_name", nullable = false, columnDefinition = "varchar(100) default '' comment '角色名称' ") private String roleName = ""; // @ManyToMany(targetEntity = User.class) // @JoinTable( // name = "sys_user_role", // //joinColumns 当前对象在中间表的外键 // joinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")}, // //inverseJoinColumns 对方对象在中间表的外键 // inverseJoinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")} // ) //放弃维护权 @ManyToMany(mappedBy = "roles") private Set<User> users = new HashSet<>(); public Set<User> getUsers() { return users; } public Role() { } public void setUsers(Set<User> users) { this.users = users; } 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; } } ~~~