mybaitsPlus 实现乐观锁配置

1.增加配置
@Configuration
public class MybatisPlusConfig {

  //注册乐观锁插件
  @Bean
  public OptimisticLockerInterceptor optimisticLockerInterceptor(){
    return new OptimisticLockerInterceptor();
  }

}
2. 修改实体类,新增@Version注解
@Version
private Integer version;
3.注意事项⚠️
  • 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
  • 仅支持updateById(id) 与 update(entity, wrapper) 方法
  • 在 update(entity, wrapper)方法下, wrapper 不能复用!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MybatisPlus是Mybatis的增强版,提供了更加强大和方便的功能,如自动生成代码、分页、多租户、逻辑删除等。下面是MybatisPlus的完整配置: pom.xml中添加依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.3</version> </dependency> ``` 配置文件application.yml中添加配置: ``` # mybatis-plus配置 mybatis-plus: # mapper.xml所在路径 mapper-locations: classpath:/mapper/*.xml # 实体类扫描路径 typeAliasesPackage: com.example.entity global-config: # 开启驼峰命名转换 db-column-underline: true id-type: auto field-strategy: not_empty # 分页配置 page-size: 10 page-sql-limit: true # 数据库配置 datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver ``` 其中,mapper-locations指定mapper.xml所在路径,typeAliasesPackage指定实体类扫描路径,db-column-underline开启驼峰命名转换,id-type指定主键生成策略,field-strategy指定自动填充策略,page-size和page-sql-limit指定分页配置,datasource指定数据库配置。 然后,在实体类上使用注解配置: ``` @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; @TableLogic private Integer deleted; } ``` 其中,@TableName指定实体类对应的表名,@TableId指定主键,@TableLogic指定逻辑删除字段。 最后,在Mapper接口上继承BaseMapper即可使用MybatisPlus提供的增删改查方法: ``` @Repository public interface UserMapper extends BaseMapper<User> { } ``` 以上就是MybatisPlus的完整配置
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值