Spring_Data_JPA关联映射操作

一对一的关联关系

需求:用户与角色的一对一的关联关系
用户:一方
角色:一方

创建Users实体类
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@Entity
@Table(name = "users")
public class Users {
// 主键自增策略,自增
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer userId;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String passWord;
@Column(name = "email")
private String email;
@OneToOne(cascade = CascadeType.PERSIST)
//@JoinColumn:就是维护一个外键
@JoinColumn(name="roles_id")
private Roles roles;

public Users(Integer userId, String userName, String passWord, String email) {
this.userId = userId;
this.userName = userName;
this.passWord = passWord;
this.email = email;
}

public Users(){}

@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", email='" + email + '\'' +
'}';
}

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 String getPassWord() {
return passWord;
}

public void setPassWord(String passWord) {
this.passWord = passWord;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Roles getRoles() {
return roles;
}

public void setRoles(Roles roles) {
this.roles = roles;
}
}
创建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
@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;
}
}

一对多的关联关系

需求:从角色到用户的一对多的关联关系
角色:一方
用户:多方

创建Users实体
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
@Entity
@Table(name="t_users")
public class Users implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)//strategy=Gene
rationType.IDENTITY 自增长
@Column(name="userid")
private Integer userid;
@Column(name="username")
private String username;
@Column(name="userage")
private Integer userage;
@ManyToOne(cascade = CascadeType.PERSIST)
@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 + "]";
}
}
创建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
@Entity
@Table(name = "roles")
public class Roles1 {
@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 + "]";
}

public Set<Users> getUsers() {
return users;
}

public void setUsers(Set<Users> users) {
this.users = users;
}
}
测试代码
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
/**
* 一对多的关联关系测试
* @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);
}
}

多对多的关联关系

需求:一个角色可以拥有多个菜单,一个菜单可以分配多个角色。多对多的关联关系
角色:多方
菜单:多方

创建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
@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="ro
le_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 + "]";
}
}
创建Menus实体
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
@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
/**
* 多对多关联关系测试
* @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);
} } }