Python 官方文档:入门教程 => 点击学习
目录springDataJpa创建联合索引创建联合索引对应类创建映射实体类添加新数据SpringDataJpa指定联合索引在@Table中指定UniqueConstraint自动加上
@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 {
// 复合主键
@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();
}
如何,现在我的表里使用订单ID和产品ID作为唯一索引,那么需要在定义表实体类时
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@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)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: SpringDataJpa创建联合索引的实现
本文链接: https://www.lsjlt.com/news/159431.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0