CREATE TABLE t_product
(
id BIGINT(20) NOT NULL COMMENT '主键id',
NAME VARCHAr(30) NULL DEFAULT NULL COMMENT '商品名称',
price INT(11) DEFAULT 0 COMMENT '价格',
VERSION INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
PRIMARY KEY (id)
);
插入一条数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人', 100);在pojo包下创建Product类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("t_product")
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
在mapper包下创建ProductMapper接口
@Repository @Mapper public interface ProductMapper extends BaseMapper冲突测试:{ }
@Autowired
private ProductMapper productMapper;
@Test
public void testProduct(){
//小李查询价格
Product productLi = productMapper.selectById(1);
System.out.println("小李查询的商品价格:"+productLi.getPrice());
//小王查询价格
Product productWang = productMapper.selectById(1);
System.out.println("小王查询的商品价格:"+productWang.getPrice());
//小李将商品价格+50
productLi.setPrice(productLi.getPrice()+50);
productMapper.updateById(productLi);
//小王将商品价格-30
productWang.setPrice(productWang.getPrice()-30);
productMapper.updateById(productWang);
//老板查询商品价格
Product productLaoan = productMapper.selectById(1);
System.out.println("老板查询的商品价格:"+productLaoan.getPrice());
}
结果:
不对
d e>mybatisplus实现乐观锁说明:
- 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
- 整数类型下 newVersion = oldVersion + 1
- newVersion 会回写到 entity 中
- 仅支持 updateById(id) 与 update(entity, wrapper) 方法
- 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("t_product")
public class Product {
private Long id;
private String name;
private Integer price;
@Version
//表示乐观锁版本号字段
private Integer version;
}
乐观锁插件配置:
spring xml 方式:
spring boot 注解方式:
在java文件夹创建一个配置文件夹config,在里面创建一个插件配置类mybatisplusconfig,添加插件
package config;
@Configuration
@MapperScan(basePackages = {"mapper"})//引导类扫描定义的接口
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
// @Bean
// public ConfigurationCustomizer configurationCustomizer() {
// return configuration -> configuration.setUsetUseDeprecatedExecutor(false);
// }
}
测试:
同上
结果:依然不对
优化测试: @Autowired
private ProductMapper productMapper;
@Test
public void testProduct(){
//小李查询价格
Product productLi = productMapper.selectById(1);
System.out.println("小李查询的商品价格:"+productLi.getPrice());
//小王查询价格
Product productWang = productMapper.selectById(1);
System.out.println("小王查询的商品价格:"+productWang.getPrice());
//小李将商品价格+50
productLi.setPrice(productLi.getPrice()+50);
productMapper.updateById(productLi);
//小王将商品价格-30
productWang.setPrice(productWang.getPrice()-30);
//判断修改的版本是否正确,result=0表示修改的版本错误,即有人刚修改了内容
int result=productMapper.updateById(productWang);
if (result==0){
//重新获取版本号,修改内容
Product productNew=productMapper.selectById(1);
productNew.setPrice(productNew.getPrice()-30);
productMapper.updateById(productNew);
}
//老板查询商品价格
Product productLaoan = productMapper.selectById(1);
System.out.println("老板查询的商品价格:"+productLaoan.getPrice());
}
结果:



