千家信息网

Spring DataJpa如何创建联合索引

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"Spring DataJpa如何创建联合索引",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Spring DataJpa如何创建联合索引"
千家信息网最后更新 2026年11月03日Spring DataJpa如何创建联合索引

本篇内容主要讲解"Spring DataJpa如何创建联合索引",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Spring DataJpa如何创建联合索引"吧!

SpringDataJpa创建联合索引

创建联合索引对应类

/** * 作者:guoyzh * 时间:2019/12/30 14:58 * 功能:戴镜视力复查联合主键 */@Data@Embeddablepublic 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@Datapublic class QyVisualReexamination {    /*@Id    @Column(nullable = true, name = "id")    private String id;    @Id    @Column(nullable = true, name = "c_review_date")    private java.sql.Timestamp cReviewDate;*/    // 复合主键    @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;}

添加新数据

@Overridepublic 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();    // 生成联合索引    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指定联合索引

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

在@Table中指定UniqueConstraint

import lombok.AllArgsConstructor;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import javax.persistence.*;/** * 产品表 * * @author wulinfeng * @since 2019/12/13 */@Entity@Table(name = "t_product", uniqueConstraints = @UniqueConstraint(columnNames = {"orderId", "productId"}))@Getter@Setter@AllArgsConstructor@NoArgsConstructorpublic class ProductItem {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    // 订单Id    @Column(nullable = false, length = 32)    private String orderId;    // 受理产品编码    @Column(length = 32)    private String productId;    // 产品名称    @Column(length = 32)    private String productName;    // 时间戳    @Column(length = 13)    private Long timestamp;}

把原来的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)

到此,相信大家对"Spring DataJpa如何创建联合索引"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

索引 联合 产品 内容 实体 时间 订单 学习 实用 更深 中指 作者 兴趣 功能 名称 实用性 实际 操作简单 数据 方法 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 华服互联网科技靠谱吗 互联网科技公司起名 北京路客互联网科技有限公司 深圳恒合互联网络科技有限公司 互联网科技公司的优势 网络科技有限互联网 中资互联网科技 与互联网科技公司 致聘互联网科技 苏州互联网科技贷款公司 大白互联网科技 四个字互联网科技公司取名 互联网科技创新企业排名 青海省互联网新科技 恒生互联网科技业指数调整周期 元气玛特互联网科技公司是外包吗 2021世界互联网领先科技成果发布 易企秀互联网科技是做什么的 恒生科技指数 中概互联网 网互联网络科技 互联网科技革命 居家互联网科技有限公司 全球顶尖互联网科技公司 洛阳昕诺飞互联网科技有限公司 恒生互联网科技业指数调整周期 互联网时代科技型公司的机会 万物互联网智能科技 唐山双旺合互联网科技产业园 科技互联网公司两年变化 互联网科技博主张洲
0