SpringDataJpa创建联合索引的实现

 更新时间:2021年12月08日 12:00:15   作者:414丶小哥  
这篇文章主要介绍了SpringDataJpa创建联合索引的实现,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringDataJpa创建联合索引

在这里插入图片描述

创建联合索引对应类

/**
 * 作者:guoyzh
 * 时间:2019/12/30 14:58
 * 功能:戴镜视力复查联合主键
 */
@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;*/
    // 复合主键
    @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();
    // 生成联合索引
    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
@NoArgsConstructor
public 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)

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

相关文章

  • response对象的使用(实例讲解)

    response对象的使用(实例讲解)

    下面小编就为大家带来一篇response对象的使用(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • java实现对对碰小游戏

    java实现对对碰小游戏

    这篇文章主要为大家详细介绍了java实现对对碰小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • java.lang.InterruptedException异常的问题解决

    java.lang.InterruptedException异常的问题解决

    本文主要介绍了java.lang.InterruptedException异常的问题解决,这种异常通常意味着 Jenkins 任务在执行过程中被中断,这可能会导致任务失败或中止,下面就来介绍一下解决方法,感兴趣的可以了解一下
    2024-07-07
  • Java8的Lambda遍历两个List匹配数据方式

    Java8的Lambda遍历两个List匹配数据方式

    这篇文章主要介绍了Java8的Lambda遍历两个List匹配数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java四种元注解介绍

    Java四种元注解介绍

    元注解是用来修饰注解的注解,在java.lang.annotation包下,当我们需要自己定义一个注解去做某些事情的时候,我们要对该注解进行一些限制,确保我们注解的作用域,这篇文章主要介绍了Java四种元注解介绍,需要的朋友可以参考下
    2024-08-08
  • Spring中@Value设置默认值问题解决

    Spring中@Value设置默认值问题解决

    本文主要介绍了Spring中@Value设置默认值问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • SpringCloud注册中心之consul详细讲解使用方法

    SpringCloud注册中心之consul详细讲解使用方法

    Consul是一款由HashiCorp公司开源的,用于服务治理的软件,Spring Cloud Consul对其进行了封装,这篇文章主要介绍了springcloud组件consul服务治理,需要的朋友可以参考下
    2022-11-11
  • Spark JDBC操作MySQL方式详细讲解

    Spark JDBC操作MySQL方式详细讲解

    这篇文章主要介绍了Spark JDBC操作MySQL方式,Spark SQL可以通过JDBC从传统的关系型数据库中读写数据,读取数据后直接生成的是DataFrame,然后再加上借助于Spark SQL丰富的API来进行各种操作
    2023-02-02
  • 零基础入门学习——Spring Boot注解(一)

    零基础入门学习——Spring Boot注解(一)

    这篇文章主要介绍了Spring Boot注解学习(一)要点,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-05-05
  •  Java图形化界面编程实现简单计算器

     Java图形化界面编程实现简单计算器

    这篇文章主要介绍了Java图形化界面编程实现简单计算器,下面文章围绕Java图形化界面编程实现简单计算器的相关资料展开详细内容,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-01-01

最新评论