您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Spring DataJpa如何創(chuàng)建聯(lián)合索引”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Spring DataJpa如何創(chuàng)建聯(lián)合索引”吧!
/** * 作者: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; }
@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; }
@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(); }
如何,現(xiàn)在我的表里使用訂單ID和產(chǎn)品ID作為唯一索引,那么需要在定義表實(shí)體類(lèi)時(shí)
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)品名稱(chēng) @Column(length = 32) private String productName; // 時(shí)間戳 @Column(length = 13) private Long timestamp; }
把原來(lái)的t_product表drop掉,重啟spring boot,再看該表
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)
到此,相信大家對(duì)“Spring DataJpa如何創(chuàng)建聯(lián)合索引”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(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)容。