Spring Data JPA 的一对多,一对多,多对多操作

时间:2022-09-19 19:50:14
一对一的关联关系
需求:用户与角色的一对一的关联关系
用户:一方
角色:一方
创建 Users 实体
@Entity
@Table(name = "t_users")
public class Users implements Serializable { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增长
@Column(name = "userid")
private Integer userid; private String username; private Integer userage; @OneToOne(cascade = CascadeType.PERSIST)
//@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "fk_roles_id")
private Roles roles;

创建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;
一对一关联关系操作
com.bjsxt.dao.UserDao
package com.bjsxt.dao;

import com.bjsxt.pojo.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface UserDao extends JpaRepository<Users,Integer>, JpaSpecificationExecutor<Users> { }

com.bjsxt.test.UserTest

   @Test
public void addUserAndRole(){
Roles roles=new Roles();
roles.setRolename("管理员"); Users users=new Users();
users.setUsername("yxf");
users.setUserage(22); //建立关系
users.setRoles(roles);
roles.setUsers(users); userDao.save(users);
一对多的关联关系
需求:从角色到用户的一对多的关联关系
角色:一方
用户:多方
创建 Users 实体
package com.bjsxt.pojo;

import javax.persistence.*;
import java.io.Serializable; @Entity
@Table(name = "t_users")
public class Users implements Serializable { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增长
@Column(name = "userid")
private Integer userid; private String username; private Integer userage; // @OneToOne(cascade = CascadeType.PERSIST)
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "fk_roles_id")
private Roles roles;
public Users(){} 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;
} public Roles getRoles() {
return roles;
} public void setRoles(Roles roles) {
this.roles = roles;
} public Users(String username, Integer userage, Roles roles) {
this.username = username;
this.userage = userage;
this.roles = roles;
} @Override
public String toString() {
return "Users{" +
"userid=" + userid +
", username='" + username + '\'' +
", userage=" + userage +
", roles=" + roles +
'}';
}
}
创建 Roles 实体
package com.bjsxt.pojo;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; @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;
@OneToMany(mappedBy = "roles",cascade = CascadeType.PERSIST)
private Set<Users> users=new HashSet<>(); public Roles(String rolename, Set<Users> users) {
this.rolename = rolename;
this.users = users;
} @Override
public String toString() {
return "Roles{" +
"roleid=" + roleid +
", rolename='" + rolename + '\'' +
", 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;
} public Set<Users> getUsers() {
return users;
} public void setUsers(Set<Users> users) {
this.users = users;
} public Roles(){} }
一对多的关联关系操作
 @Test
public void addUserAndRole(){
Roles roles=new Roles();
roles.setRolename("班长"); Users users=new Users();
users.setUsername("xm");
users.setUserage(22); Users users1=new Users();
users1.setUsername("hw");
users1.setUserage(23);
Set<Users> set = roles.getUsers(); set.add(users);
set.add(users1); users.setRoles(roles);
users1.setRoles(roles); userDao.save(users);
userDao.save(users1); }
多对多的关联关系
需求:一个角色可以拥有多个菜单,一个菜单可以分配多个角色。多对多的关联关系
角色:多方
菜单:多方
创建 Roles 实体
package com.bjsxt.pojo;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; @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(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
@JoinTable(name = "t_menus_roles",joinColumns = @JoinColumn(name = "role_id"),inverseJoinColumns = @JoinColumn(name = "menu_id"))
private Set<Menus> menus=new HashSet<>(); public Roles(){} @Override
public String toString() {
return "Roles{" +
"roleid=" + roleid +
", rolename='" + rolename + '\'' +
", 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;
} public Set<Menus> getMenus() {
return menus;
} public void setMenus(Set<Menus> menus) {
this.menus = menus;
} public Roles(String rolename, Set<Menus> menus) {
this.rolename = rolename;
this.menus = menus;
}
}
创建 Menus 实体
package com.bjsxt.pojo;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; @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 Menus(){} @Override
public String toString() {
return "Menus{" +
"menusid=" + menusid +
", menusname='" + menusname + '\'' +
", menusurl='" + menusurl + '\'' +
", fatherid=" + fatherid +
", 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;
} public Set<Roles> getRoles() {
return roles;
} public void setRoles(Set<Roles> roles) {
this.roles = roles;
} public Menus(String menusname, String menusurl, Integer fatherid, Set<Roles> roles) {
this.menusname = menusname;
this.menusurl = menusurl;
this.fatherid = fatherid;
this.roles = roles;
}
}
多对多关联关系操作
package com.bjsxt.test;

import com.bjsxt.dao.RolesDao;
import com.bjsxt.pojo.Menus;
import com.bjsxt.pojo.Roles;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class RolesTest { @Autowired
private RolesDao rolesDao; @Test
public void addRoles(){
Roles roles=new Roles();
roles.setRolename("超级管理员"); Menus menus1=new Menus();
menus1.setFatherid(-1);
menus1.setMenusname("***管理系统");
menus1.setMenusurl(null); Menus menus2=new Menus();
menus2.setFatherid(1);
menus2.setMenusname("用户管理");
menus2.setMenusurl(null); roles.getMenus().add(menus1);
roles.getMenus().add(menus2);
menus1.getRoles().add(roles);
menus2.getRoles().add(roles); rolesDao.save(roles);
} @Test
public void findall(){
Roles roles = rolesDao.findOne(10);
System.out.println("角色信息:"+roles);
Set<Menus> menus = roles.getMenus();
for (Menus menu : menus) {
System.out.println("菜单信息:"+menu); }
}
}