SpringDataJpa創(chuàng)建聯(lián)合索引的實(shí)現(xiàn)
SpringDataJpa創(chuàng)建聯(lián)合索引
創(chuàng)建聯(lián)合索引對應(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í)體類json數(shù)據(jù)時(shí)忽略某個(gè)屬性方法
這篇文章主要給大家介紹了關(guān)于java返回前端實(shí)體類json數(shù)據(jù)時(shí)忽略某個(gè)屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08利用json2POJO with Lombok 插件自動(dòng)生成java類的操作
這篇文章主要介紹了利用json2POJO with Lombok 插件自動(dòng)生成java類的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12詳解SpringBoot如何刪除引用jar包中的無用bean
為了趕速度和直接將之前多模塊的maven項(xiàng)目中的部分模塊,直接以jar包的形式引入到新項(xiàng)目中了,雖然省去了不少開發(fā)時(shí)間,導(dǎo)致項(xiàng)目臃腫,啟動(dòng)很慢。本文將用@ComponentScan注解去實(shí)現(xiàn)讓項(xiàng)目只加載自己需要的bean,需要的可以參考一下2022-06-06動(dòng)態(tài)上傳jar包熱部署的實(shí)戰(zhàn)詳解
開發(fā)系統(tǒng)過程中遇到的一個(gè)需求,系統(tǒng)給定一個(gè)接口,用戶可以自定義開發(fā)該接口的實(shí)現(xiàn),并將實(shí)現(xiàn)打成jar包,上傳到系統(tǒng)中。系統(tǒng)完成熱部署,并切換該接口的實(shí)現(xiàn)。本文詳細(xì)介紹了實(shí)現(xiàn)方法,需要的可以參考一下2022-10-10在Intellij Idea中使用jstl標(biāo)簽庫的方法
這篇文章主要介紹了在Intellij Idea中使用jstl標(biāo)簽庫的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05java并發(fā)編程之深入理解Synchronized的使用
文詳細(xì)講述了線程、進(jìn)程的關(guān)系及在操作系統(tǒng)中的表現(xiàn),這是多線程學(xué)習(xí)必須了解的基礎(chǔ)。本文將接著講一下Java線程同步中的一個(gè)重要的概念synchronized,希望能夠給你有所幫助2021-06-06SpringBoot配置使Mybatis打印SQL執(zhí)行時(shí)的實(shí)際參數(shù)值操作
這篇文章主要介紹了SpringBoot配置使Mybatis打印SQL執(zhí)行時(shí)的實(shí)際參數(shù)值操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12