1、主键策略
@TableId(value = "id",type = IdType.AUTO)//设置主键自增 这里需要注意,设置主键自增的时候不要忘记数据库也要设置主键自增
private Long id;
官网:主键策略 | MyBatis-Plus (baomidou.com)
2、乐观锁插件
乐观锁:
1、先查询,获得版本号 version = 1
-- A
update user set name = "hekun", version = version + 1 where id = 2 and version = 1
-- B 线程抢先完成,这个时候 version = 2,会导致 A 修改失败!
update user set name = "hekun", version = version + 1 where id = 2 and version = 1
version类型是int类型,并且不为空
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
@Version
private Integer version;
官网:乐观锁插件 | MyBatis-Plus (baomidou.com)
3、逻辑删除
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)