mybatis-plus中主键填充策略

mybatis-plus中主键id默认使用雪花算法生成的唯一id,在实际业务中需要自定义时,可以修改其填充策略。

mybatis-plus实体类实现主键策略有三种(注解>全局>默认)

1. 局部主键策略实现

在实体类中ID属性加注解

@TableId(type = IdType.AUTO) 主键自增 数据库中需要设置主键自增
private Long userId;

@TableId(type = IdType.NONE) 默认 跟随全局策略走
private Long userId;

@TableId(type = IdType.UUID) UUID类型主键
private Long userId;

@TableId(type = IdType.ID_WORKER) 数值类型 数据库中也必须是数值类型 否则会报错
private Long userId;

@TableId(type = IdType.ID_WORKER_STR) 字符串类型 数据库也要保证一样字符类型
private Long userId;

@TableId(type = IdType.INPUT) 用户自定义了 数据类型和数据库保持一致就行
private Long userId;

2. 全局主键策略实现

需要在application.yml文件中,添加配置

mybatis-plus:
mapper-locations:
- com/mp/mapper/*
global-config:
db-config:
id-type: uuid/none/input/id_worker/id_worker_str/auto 表示全局主键都采用该策略(如果全局策略和局部策略都有设置,局部策略优先级高)

mybatis-plus配置代码生成器参考博客:https://blog.csdn.net/duan196_118/article/details/111691248

猜你喜欢

转载自blog.csdn.net/duan196_118/article/details/111592835