前言
這次大創(chuàng)有個(gè)需求,在數(shù)據(jù)庫(kù)建表時(shí)發(fā)現(xiàn),user表與project表的關(guān)系表 user_project的主鍵為復(fù)合主鍵:
1
2
3
4
5
6
7
|
create table user_project( user_id int ( 20 ), project_id int ( 20 ), timestamp varchar ( 50 ), donate_money double ( 10 , 2 ), primary key (user_id,project_id) ); |
在網(wǎng)上看了幾篇博客,以及在spring boot干貨群咨詢(感謝夜升額耐心解答)過后總算是做出來(lái)了。這里做個(gè)總結(jié),方便日后查閱。
正文
這里采用@idclass注解的方式來(lái)實(shí)現(xiàn)復(fù)合主鍵;
思路
- 編寫一個(gè)復(fù)合主鍵類userprojectmultikeysclass;
- 通過@idclass注釋在實(shí)體中標(biāo)注復(fù)合主鍵;
- 可以通過entitymanager獲取數(shù)據(jù),或者是直接在repository 里寫方法;
實(shí)現(xiàn)
復(fù)合主鍵類
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.hzy.model; import java.io.serializable; /** * created by huangzhenyang on 2017/9/7. * userproject的復(fù)合主鍵類 * * @param userid * @param projectid * @param timestamp * 由這三個(gè)共同組成復(fù)合主鍵 */ public class userprojectmultikeysclass implements serializable { private integer userid; private integer projectid; private string timestamp; //constructor public userprojectmultikeysclass() { } public userprojectmultikeysclass(integer userid, integer projectid, string timestamp) { this .userid = userid; this .projectid = projectid; this .timestamp = timestamp; } //setter and getter public integer getuserid() { return userid; } public void setuserid(integer userid) { this .userid = userid; } public integer getprojectid() { return projectid; } public void setprojectid(integer projectid) { this .projectid = projectid; } public string gettimestamp() { return timestamp; } public void settimestamp(string timestamp) { this .timestamp = timestamp; } // ***重寫hashcode與equals方法*** 劃重點(diǎn)! @override public int hashcode() { final int prime = 31 ; int result = 1 ; result = prime * result + ((userid == null ) ? 0 : userid.hashcode()); result = prime * result + ((projectid == null ) ? 0 : projectid.hashcode()); result = prime * result + ((timestamp == null ) ? 0 : timestamp.hashcode()); return result; } @override public boolean equals(object obj){ if ( this == obj){ return true ; } if (obj == null ){ return false ; } if (getclass() != obj.getclass()){ return false ; } final userprojectmultikeysclass other = (userprojectmultikeysclass)obj; if (userid == null ){ if (other.userid != null ){ return false ; } } else if (!userid.equals(other.userid)){ return false ; } if (projectid == null ){ if (other.projectid != null ){ return false ; } } else if (!projectid.equals(other.projectid)){ return false ; } if (timestamp == null ){ if (other.timestamp != null ){ return false ; } } else if (!timestamp.equals(other.timestamp)){ return false ; } return true ; } } |
注意:
復(fù)合主鍵類必須滿足:
1. 實(shí)現(xiàn)serializable接口;
2. 有默認(rèn)的public無(wú)參數(shù)的構(gòu)造方法;
3. 重寫equals和hashcode方法。equals方法用于判斷兩個(gè)對(duì)象是否相同,entitymanger通過find方法來(lái)查找entity時(shí),是根據(jù)equals的返回值來(lái)判斷的。hashcode方法返回當(dāng)前對(duì)象的哈希碼;
實(shí)體類
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
|
package com.hzy.model; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.idclass; import javax.persistence.table; import java.io.serializable; /** * created by huangzhenyang on 2017/9/7. * */ @entity @table (name = "user_project" ) @idclass (userprojectmultikeysclass. class ) public class userproject implements serializable { private double donatemoney; private integer userid; private integer projectid; private string timestamp; @id public integer getuserid(){ return this .userid; } @id public integer getprojectid(){ return this .projectid; } @id public string gettimestamp(){ return this .timestamp; } //getter and setter public double getdonatemoney() { return donatemoney; } public void setdonatemoney( double donatemoney) { this .donatemoney = donatemoney; } public void setuserid(integer userid) { this .userid = userid; } public void setprojectid(integer projectid) { this .projectid = projectid; } public void settimestamp(string timestamp) { this .timestamp = timestamp; } @override public string tostring() { return "userproject{" + "donatemoney=" + donatemoney + ", userid=" + userid + ", projectid=" + projectid + ", timestamp='" + timestamp + '\ '' + '}' ; } } |
注意:
1. @idclass標(biāo)注用于標(biāo)注實(shí)體所使用主鍵規(guī)則的類;
2. 在實(shí)體中同時(shí)標(biāo)注主鍵的屬性,比如這段代碼中的userid,projectid以及timestamp ;
獲取數(shù)據(jù)
方法一: 通過entitymanager獲取,比如方法testuserprojectrepository()
方法二:通過repository獲取;這里記得在extends jparepository<userproject,userprojectmultikeysclass>時(shí)把id的主鍵類指定為復(fù)合主鍵類userprojectmultikeysclass
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public interface userprojectrepository extends jparepository<userproject,userprojectmultikeysclass>{ // 根據(jù)用戶id,找出用戶參與的所有userproject // test pass list<userproject> findbyuserid(integer userid); // 根據(jù)項(xiàng)目id,找出參與項(xiàng)目的所有userproject // test pass list<userproject> findbyprojectid(integer projectid); // 根據(jù)用戶id和項(xiàng)目id 找出所有的userproject // test pass list<userproject> findbyuseridandprojectid(integer userid,integer projectid); } |
單元測(cè)試的代碼
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
|
package com.hzy; import com.hzy.model.userproject; import com.hzy.model.userprojectmultikeysclass; import com.hzy.repository.userprojectrepository; import com.hzy.service.userprojectservice; import com.hzy.service.userservice; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.transaction.transactional; import java.util.list; /** * created by huangzhenyang on 2017/9/8. */ @runwith (springrunner. class ) @springboottest public class userproejctrepositorytest { @autowired @persistencecontext private entitymanager entitymanager; @autowired private userprojectrepository userprojectrepository; @test public void testuserprojectrepository(){ userprojectmultikeysclass userprojectmultikeysclass = new userprojectmultikeysclass( 1 , 1 , "2017-09-08" ); userproject userproject = entitymanager.find(userproject. class ,userprojectmultikeysclass); system.out.println(userproject.tostring()); } @test public void testfindbyuserid(){ list<userproject> userprojects = userprojectrepository.findbyuserid( 1 ); for (userproject userproject:userprojects){ system.out.println(userproject.tostring()); } } @test public void testfindbyprojectid(){ list<userproject> userprojects = userprojectrepository.findbyprojectid( 1 ); for (userproject userproject:userprojects){ system.out.println(userproject.tostring()); } } @test public void testfindbyuseridandprojectid(){ list<userproject> userprojects = userprojectrepository.findbyuseridandprojectid( 1 , 1 ); for (userproject userproject:userprojects){ system.out.println(userproject.tostring()); } } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_35056292/article/details/77892012