您好,登錄后才能下訂單哦!
前言
這次大創(chuàng)有個(gè)需求,在數(shù)據(jù)庫(kù)建表時(shí)發(fā)現(xiàn),user表與project表的關(guān)系表 user_project的主鍵為復(fù)合主鍵:
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干貨群咨詢(感謝夜升額耐心解答)過(guò)后總算是做出來(lái)了。這里做個(gè)總結(jié),方便日后查閱。
正文
這里采用@IdClass注解的方式來(lái)實(shí)現(xiàn)復(fù)合主鍵;
思路
實(shí)現(xiàn)
復(fù)合主鍵類
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; } // ***重寫(xiě)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. 重寫(xiě)equals和hashCode方法。equals方法用于判斷兩個(gè)對(duì)象是否相同,EntityManger通過(guò)find方法來(lái)查找Entity時(shí),是根據(jù)equals的返回值來(lái)判斷的。hashCode方法返回當(dāng)前對(duì)象的哈希碼;
實(shí)體類
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ù)
方法一: 通過(guò)EntityManager獲取,比如方法testUserProjectRepository()
方法二:通過(guò)Repository獲取;這里記得在extends JpaRepository<UserProject,UserProjectMultiKeysClass>時(shí)把id的主鍵類指定為復(fù)合主鍵類UserProjectMultiKeysClass
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è)試的代碼
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í)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。