经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringDataJpa创建联合索引的实现
来源:jb51  时间:2021/12/8 14:59:25  对本文有异议

SpringDataJpa创建联合索引

在这里插入图片描述

创建联合索引对应类

  1. /**
  2. * 作者:guoyzh
  3. * 时间:2019/12/30 14:58
  4. * 功能:戴镜视力复查联合主键
  5. */
  6. @Data
  7. @Embeddable
  8. public class VisualReexaminationUnionKey implements Serializable {
  9. @Column(name = "id")
  10. private String id;
  11. @Column(name = "c_review_date")
  12. private java.sql.Timestamp cReviewDate;
  13. }

创建映射实体类

  1. @Table(name = "qy_visual_reexamination")
  2. @Entity
  3. @Data
  4. public class QyVisualReexamination {
  5. /*@Id
  6. @Column(nullable = true, name = "id")
  7. private String id;
  8. @Id
  9. @Column(nullable = true, name = "c_review_date")
  10. private java.sql.Timestamp cReviewDate;*/
  11. // 复合主键
  12. @EmbeddedId
  13. private VisualReexaminationUnionKey id;
  14. @Column(nullable = true, name = "c_clientid")
  15. private String cClientid;
  16. @Column(nullable = true, name = "c_ygscode")
  17. private String cYgscode;
  18. @Column(nullable = true, name = "c_primary_vision_r")
  19. private String cPrimaryVisionR;
  20. @Column(nullable = true, name = "c_primary_vision_l")
  21. private String cPrimaryVisionL;
  22. @Column(nullable = true, name = "c_ball_r")
  23. private String cBallR;
  24. @Column(nullable = true, name = "c_ball_l")
  25. private String cBallL;
  26. @Column(nullable = true, name = "c_pole_r")
  27. private String cPoleR;
  28. @Column(nullable = true, name = "c_pole_l")
  29. private String cPoleL;
  30. @Column(nullable = true, name = "c_axes_r")
  31. private String cAxesR;
  32. @Column(nullable = true, name = "c_axes_l")
  33. private String cAxesL;
  34. @Column(nullable = true, name = "c_add_r")
  35. private String cAddR;
  36. @Column(nullable = true, name = "c_add_l")
  37. private String cAddL;
  38. @Column(nullable = true, name = "c_check_r")
  39. private String cCheckR;
  40. @Column(nullable = true, name = "c_check_l")
  41. private String cCheckL;
  42. @Column(nullable = true, name = "c_proposal")
  43. private String cProposal;
  44. @Column(nullable = true, name = "c_com")
  45. private String cCom;
  46. }

添加新数据

  1. @Override
  2. public Object addVisualReexamination(String id, String clientId, String reviewDate, String ygsCode, String primaryVisionR,
  3. String primaryVisionL, String ballR, String ballL, String poleR, String poleL, String axesR,
  4. String axesL, String addR, String addL, String checkR, String checkL, String proposal, String comId) {
  5. QyVisualReexamination bean = new QyVisualReexamination();
  6. // 生成联合索引
  7. VisualReexaminationUnionKey unionId = new VisualReexaminationUnionKey();
  8. unionId.setCReviewDate(Timestamp.valueOf(reviewDate));
  9. unionId.setId(id);
  10. bean.setId(unionId);
  11. bean.setCClientid(clientId);
  12. bean.setCYgscode(ygsCode);
  13. bean.setCPrimaryVisionR(primaryVisionR);
  14. bean.setCPrimaryVisionL(primaryVisionL);
  15. bean.setCBallR(ballR);
  16. bean.setCBallL(ballL);
  17. bean.setCPoleR(poleR);
  18. bean.setCPoleL(poleL);
  19. bean.setCAxesR(axesR);
  20. bean.setCAxesL(axesL);
  21. bean.setCAddR(addR);
  22. bean.setCAddL(addL);
  23. bean.setCCom(comId);
  24. bean.setCCheckR(checkR);
  25. bean.setCCheckL(checkL);
  26. bean.setCProposal(proposal);
  27. QyVisualReexamination save = mQyVisualReexaminationDao.save(bean);
  28. return save.getId();
  29. }

SpringDataJpa指定联合索引

如何,现在我的表里使用订单ID和产品ID作为唯一索引,那么需要在定义表实体类时

在@Table中指定UniqueConstraint

  1. import lombok.AllArgsConstructor;
  2. import lombok.Getter;
  3. import lombok.NoArgsConstructor;
  4. import lombok.Setter;
  5. import javax.persistence.*;
  6. /**
  7. * 产品表
  8. *
  9. * @author wulinfeng
  10. * @since 2019/12/13
  11. */
  12. @Entity
  13. @Table(name = "t_product", uniqueConstraints = @UniqueConstraint(columnNames = {"orderId", "productId"}))
  14. @Getter
  15. @Setter
  16. @AllArgsConstructor
  17. @NoArgsConstructor
  18. public class ProductItem {
  19. @Id
  20. @GeneratedValue(strategy = GenerationType.IDENTITY)
  21. private Long id;
  22. // 订单Id
  23. @Column(nullable = false, length = 32)
  24. private String orderId;
  25. // 受理产品编码
  26. @Column(length = 32)
  27. private String productId;
  28. // 产品名称
  29. @Column(length = 32)
  30. private String productName;
  31. // 时间戳
  32. @Column(length = 13)
  33. private Long timestamp;
  34. }

把原来的t_product表drop掉,重启spring boot,再看该表

自动加上唯一索引了

  1. mysql> show index from t_product;
  2. +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  3. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  4. +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  5. | t_product | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
  6. | t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 1 | order_id | A | 2 | NULL | NULL | | BTREE | | |
  7. | t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 2 | product_id | A | 2 | NULL | NULL | YES | BTREE | | |
  8. +-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  9. 3 rows in set (0.00 sec)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号