jeecg boot 乐观锁使用

nankui
2022-03-23 / 0 评论 / 1,052 阅读 / 正在检测是否收录...

mybatis-plus 乐观锁
当要更新一条记录的时候,希望这条记录没有被别人更新

spring boot 注解方式

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 */
@Configuration
@MapperScan(value={"org.jeecg.modules.**.mapper*"})
public class MybatisPlusConfig {

    /**
     * 新版
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusOptimisticLocking() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

在实体类的字段上加上@Version注解

@Version
private Integer version;


测试:

  @PostMapping(value = "/getCarouselList11")
    public Result<?> getCarouselList12() {
        CompletableFuture<String> a = asyncService.doSomething("我是改的第一个");
        CompletableFuture<String> b = asyncService.doSomething("我是改的第二个");
        String result=null;
        CompletableFuture.allOf(a, b).join();
        try {
            result = a.get() + b.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return Result.OK("获取成功",result);
    } 
  

0

评论 (0)

取消