SpringDataJpa創(chuàng)建聯(lián)合索引的實(shí)現(xiàn)
SpringDataJpa創(chuàng)建聯(lián)合索引

創(chuàng)建聯(lián)合索引對(duì)應(yīng)類
/**
* 作者:guoyzh
* 時(shí)間:2019/12/30 14:58
* 功能:戴鏡視力復(fù)查聯(lián)合主鍵
*/
@Data
@Embeddable
public class VisualReexaminationUnionKey implements Serializable {
@Column(name = "id")
private String id;
@Column(name = "c_review_date")
private java.sql.Timestamp cReviewDate;
}
創(chuàng)建映射實(shí)體類
@Table(name = "qy_visual_reexamination")
@Entity
@Data
public class QyVisualReexamination {
/*@Id
@Column(nullable = true, name = "id")
private String id;
@Id
@Column(nullable = true, name = "c_review_date")
private java.sql.Timestamp cReviewDate;*/
// 復(fù)合主鍵
@EmbeddedId
private VisualReexaminationUnionKey id;
@Column(nullable = true, name = "c_clientid")
private String cClientid;
@Column(nullable = true, name = "c_ygscode")
private String cYgscode;
@Column(nullable = true, name = "c_primary_vision_r")
private String cPrimaryVisionR;
@Column(nullable = true, name = "c_primary_vision_l")
private String cPrimaryVisionL;
@Column(nullable = true, name = "c_ball_r")
private String cBallR;
@Column(nullable = true, name = "c_ball_l")
private String cBallL;
@Column(nullable = true, name = "c_pole_r")
private String cPoleR;
@Column(nullable = true, name = "c_pole_l")
private String cPoleL;
@Column(nullable = true, name = "c_axes_r")
private String cAxesR;
@Column(nullable = true, name = "c_axes_l")
private String cAxesL;
@Column(nullable = true, name = "c_add_r")
private String cAddR;
@Column(nullable = true, name = "c_add_l")
private String cAddL;
@Column(nullable = true, name = "c_check_r")
private String cCheckR;
@Column(nullable = true, name = "c_check_l")
private String cCheckL;
@Column(nullable = true, name = "c_proposal")
private String cProposal;
@Column(nullable = true, name = "c_com")
private String cCom;
}
添加新數(shù)據(jù)
@Override
public Object addVisualReexamination(String id, String clientId, String reviewDate, String ygsCode, String primaryVisionR,
String primaryVisionL, String ballR, String ballL, String poleR, String poleL, String axesR,
String axesL, String addR, String addL, String checkR, String checkL, String proposal, String comId) {
QyVisualReexamination bean = new QyVisualReexamination();
// 生成聯(lián)合索引
VisualReexaminationUnionKey unionId = new VisualReexaminationUnionKey();
unionId.setCReviewDate(Timestamp.valueOf(reviewDate));
unionId.setId(id);
bean.setId(unionId);
bean.setCClientid(clientId);
bean.setCYgscode(ygsCode);
bean.setCPrimaryVisionR(primaryVisionR);
bean.setCPrimaryVisionL(primaryVisionL);
bean.setCBallR(ballR);
bean.setCBallL(ballL);
bean.setCPoleR(poleR);
bean.setCPoleL(poleL);
bean.setCAxesR(axesR);
bean.setCAxesL(axesL);
bean.setCAddR(addR);
bean.setCAddL(addL);
bean.setCCom(comId);
bean.setCCheckR(checkR);
bean.setCCheckL(checkL);
bean.setCProposal(proposal);
QyVisualReexamination save = mQyVisualReexaminationDao.save(bean);
return save.getId();
}
SpringDataJpa指定聯(lián)合索引
如何,現(xiàn)在我的表里使用訂單ID和產(chǎn)品ID作為唯一索引,那么需要在定義表實(shí)體類時(shí)
在@Table中指定UniqueConstraint
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
/**
* 產(chǎn)品表
*
* @author wulinfeng
* @since 2019/12/13
*/
@Entity
@Table(name = "t_product", uniqueConstraints = @UniqueConstraint(columnNames = {"orderId", "productId"}))
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// 訂單Id
@Column(nullable = false, length = 32)
private String orderId;
// 受理產(chǎn)品編碼
@Column(length = 32)
private String productId;
// 產(chǎn)品名稱
@Column(length = 32)
private String productName;
// 時(shí)間戳
@Column(length = 13)
private Long timestamp;
}
把原來的t_product表drop掉,重啟spring boot,再看該表
自動(dòng)加上唯一索引了
mysql> show index from t_product; +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | t_product | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | | | t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 1 | order_id | A | 2 | NULL | NULL | | BTREE | | | | t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 2 | product_id | A | 2 | NULL | NULL | YES | BTREE | | | +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)對(duì)對(duì)碰小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)對(duì)對(duì)碰小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
java.lang.InterruptedException異常的問題解決
本文主要介紹了java.lang.InterruptedException異常的問題解決,這種異常通常意味著 Jenkins 任務(wù)在執(zhí)行過程中被中斷,這可能會(huì)導(dǎo)致任務(wù)失敗或中止,下面就來介紹一下解決方法,感興趣的可以了解一下2024-07-07
Java8的Lambda遍歷兩個(gè)List匹配數(shù)據(jù)方式
這篇文章主要介紹了Java8的Lambda遍歷兩個(gè)List匹配數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring中@Value設(shè)置默認(rèn)值問題解決
本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringCloud注冊(cè)中心之consul詳細(xì)講解使用方法
Consul是一款由HashiCorp公司開源的,用于服務(wù)治理的軟件,Spring Cloud Consul對(duì)其進(jìn)行了封裝,這篇文章主要介紹了springcloud組件consul服務(wù)治理,需要的朋友可以參考下2022-11-11
零基礎(chǔ)入門學(xué)習(xí)——Spring Boot注解(一)
這篇文章主要介紹了Spring Boot注解學(xué)習(xí)(一)要點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-05-05
?Java圖形化界面編程實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要介紹了Java圖形化界面編程實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,下面文章圍繞Java圖形化界面編程實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-01-01

