Spring Boot JPA 使用 @Index 为字段添加索引

Spring Boot JPA About 566 words

需求

使用JPA创建:唯一索引、普通索引、联合索引。

代码

使用@Table中的indexes属性,指定@Index索引。

@Setter
@Getter
@Entity
@Table(name = "user", indexes = {
    @Index(name = "uk_id_card", columnList = "id_card", unique = true),
    @Index(name = "idx_name", columnList = "name"),
    @Index(name = "idx_name_id_card", columnList = "name, id_card"), // 组合索引
})
public class User extends CommonEntity {

    @Column(length = 50, nullable = false)
    private String name;

    @Column(name = "id_card")
    private String idCard;

}

说明

@Indexname指定索引名称,columnList指定添加索引的列,联合索引使用,逗号隔开,uniquetrue表示唯一索引。

Views: 2,651 · Posted: 2023-02-25

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh