SpringDataJPA之关联关系
【摘要】
本文介绍下SpringDataJPA怎么处理关联关系
关联关系处理
1.一对一
用户和角色设置一对一关系,通过外键关联。
pojo设置
创建 Users 实体
@Entity
@Tab...
本文介绍下SpringDataJPA怎么处理关联关系
关联关系处理
1.一对一
用户和角色设置一对一关系,通过外键关联。
pojo设置
创建 Users 实体
@Entity
@Table(name="t_users")
public class Users implements Serializable{
@Id
//strategy=GenerationType.IDENTITY 自增长
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="userid")
private Integer userid;
@Column(name="username")
private String username;
@Column(name="userage")
private Integer userage;
public Roles getRoles() {
return roles;
}
public void setRoles(Roles roles) {
this.roles = roles;
}
@OneToOne
//@JoinColumn:就是维护一个外键
@JoinColumn(name="roles_id")
private Roles roles;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.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;
}
@Override
public String toString() {
return "Users [userid=" + userid + ", username=" + username + ",userage=" + userage + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
创建 Roles 实体
@Entity
@Table(name="t_roles")
public class Roles {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="roleid")
private Integer roleid;
@Column(name="rolename")
private String rolename;
@OneToOne(mappedBy="roles")
private Users users;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
关联关系操作
/**
* 一对一关联关系测试
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class OneToOneTest {
@Autowired
private UsersDao usersDao;
/**
* 添加用户同时添加角色
*/
@Test
public void test1(){
//创建角色
Roles roles = new Roles();
roles.setRolename("管理员");
//创建用户
Users users = new Users();
users.setUserage(30);
users.setUsername("赵小刚");
//建立关系
users.setRoles(roles);
roles.setUsers(users);
//保存数据
this.usersDao.save(users);
}
/**
* 根据用户 ID 查询用户,同时查询用户角色
*/
@Test
public void test2(){
Users users = this.usersDao.findOne(13);
System.out.println("用户信息:"+users);
Roles roles = users.getRoles();
System.out.println(roles);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
2.一对多
从角色到用户的一对多的关联关系
角色:一方
用户:多方
pojo
@Entity
@Table(name="t_users")
public class Users implements Serializable{
@Id
//strategy=GenerationType.IDENTITY 自增长
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="userid")
private Integer userid;
@Column(name="username")
private String username;
@Column(name="userage")
private Integer userage;
@ManyToOne()
@JoinColumn(name="roles_id")
private Roles roles;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.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;
}
@Override
public String toString() {
return "Users [userid=" + userid + ", username=" + username + ",userage=" + userage + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
@Entity
@Table(name="t_roles")
public class Roles {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="roleid")
private Integer roleid;
@Column(name="rolename")
private String rolename;
@OneToMany(mappedBy="roles")
private Set<Users> users = new HashSet<>();
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
@Override
public String toString() {
return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
操作
/**
* 一对多的关联关系测试
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class OneToManyTest {
@Autowired
private UsersDao usersDao;
/**
* 添加用户同时添加角色
*/
@Test
public void test1(){
//创建角色
Roles roles = new Roles();
roles.setRolename("管理员");
//创建用户
Users users =new Users();
users.setUserage(30);
users.setUsername("小王");
//建立关系
roles.getUsers().add(users);
users.setRoles(roles);
//保存数据
this.usersDao.save(users);
}
/**
* 根据用户 ID 查询用户信息,同时查询角色
*/
@Test
public void test2(){
Users users = this.usersDao.findOne(14);
System.out.println("用户姓名:"+users.getUsername());
Roles roles = users.getRoles();
System.out.println(roles);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
3.多对多
一个角色可以拥有多个菜单,一个菜单可以分配多个角色。多对多的关联关系
角色:多方
菜单:多方
Pojo
@Entity
@Table(name="t_roles")
public class Roles {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="roleid")
private Integer roleid;
@Column(name="rolename")
private String rolename;
@ManyToMany
//@JoinTable:配置中间表信息
//joinColumns:建立当前表在中间表中的外键字段
@JoinTable(name="t_roles_menus",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="menu_id"))
private Set<Menus> menus = new HashSet<>();
public Set<Menus> getMenus() {
return menus;
}
public void setMenus(Set<Menus> menus) {
this.menus = menus;
}
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
@Override
public String toString() {
return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
@Entity
@Table(name="t_menus")
public class Menus {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="menusid")
private Integer menusid;
@Column(name="menusname")
private String menusname;
@Column(name="menusurl")
private String menusurl;
@Column(name="fatherid")
private Integer fatherid;
@ManyToMany(mappedBy="menus")
private Set<Roles> roles = new HashSet<>();
public Set<Roles> getRoles() {
return roles;
}
public void setRoles(Set<Roles> roles) {
this.roles = roles;
}
public Integer getMenusid() {
return menusid;
}
public void setMenusid(Integer menusid) {
this.menusid = menusid;
}
public String getMenusname() {
return menusname;
}
public void setMenusname(String menusname) {
this.menusname = menusname;
}
public String getMenusurl() {
return menusurl;
}
public void setMenusurl(String menusurl) {
this.menusurl = menusurl;
}
public Integer getFatherid() {
return fatherid;
}
public void setFatherid(Integer fatherid) {
this.fatherid = fatherid;
}
@Override
public String toString() {
return "Menus [menusid=" + menusid + ", menusname=" + menusname+ ", menusurl=" + menusurl + ", fatherid="+ fatherid + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
操作
/**
* 多对多关联关系测试
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ManyToManyTest {
@Autowired
private RolesDao rolesDao;
/**
* 添加角色同时添加菜单
*/
@Test
public void test1(){
//创建角色对象
Roles roles = new Roles();
roles.setRolename("超级管理员");
//创建菜单对象 XXX 管理平台 --->用户管理
Menus menus = new Menus();
menus.setMenusname("XXX 管理平台");
menus.setFatherid(-1);
menus.setMenusurl(null);
//用户管理菜单
Menus menus1 = new Menus();
menus1.setMenusname("用户管理");
menus1.setFatherid(1);
menus1.setMenusurl(null);
//建立关系
roles.getMenus().add(menus);
roles.getMenus().add(menus1);
menus.getRoles().add(roles);
menus1.getRoles().add(roles);
//保存数据
this.rolesDao.save(roles);
}
/**
* 查询 Roles
*/
@Test
public void test2(){
Roles roles = this.rolesDao.findOne(3);
System.out.println("角色信息:"+roles);
Set<Menus> menus = roles.getMenus();
for (Menus menus2 : menus) {
System.out.println("菜单信息:"+menus2);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
搞定~
文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。
原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/90320757
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)