Springboot2整合Mybatisplus实现乐观锁

1.添加pom文件

  

        
            mysql
            mysql-connector-java
            runtime
        

        
            org.projectlombok
            lombok
            true
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.0
        
        
        
            com.alibaba
            druid
            1.1.6
        

2.配置文件application.properties

server.port=8088

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

3.编写Mybatis-puls配置类

@Configuration
public class MybatisPlusConfig {

    //乐观锁配置
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
  //下面随意配置
    /**
     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    

    @Bean
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }

    //逻辑删除
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

4.编写entity对象

@Data  //注意需要下载lombok插件,不行就写get和set方法
@TableName("bank")
public class Bank implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableId(type = IdType.AUTO)
    private Integer id;

    private Integer amount;

    @Version
    private Integer version;

    private Integer userId;

}

5.编写Mapper

@Mapper
public  interface BankMapper extends BaseMapper {
}

6.编写Service

public interface BankSerice extends IService {
}

7.编写实现层,根据业务场景去写,这里不详细说明了

@Service
public class BankSeviceImpl extends ServiceImpl implements BankSerice {
    
}

8.编写控制层

@RestController
public class BankController {

    @Autowired
    BankSerice bankService;

    @RequestMapping("/bank/{userId}/{money}")
    public String access(@PathVariable("userId") String userId, @PathVariable("money") Integer money) throws InterruptedException {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Bank bank = bankService.getOne(queryWrapper);
        Integer amount = bank.getAmount();
        bank.setAmount(amount + money);
        Thread.sleep(5000);
        boolean b = bankService.updateById(bank);
        if (b) {
            return "更新成功";
        }
        return "更新失败,余额已被其他操作人员修改,请重试";
    }
}

9.数据库执行SQL

CREATE TABLE `bank` (
  `id` INT(11) NOT NULL,
  `amount` INT(10) DEFAULT NULL COMMENT '余额',
  `version` INT(5) DEFAULT NULL COMMENT '版本号',
  `user_id` INT(11) DEFAULT NULL COMMENT '用户id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=INNODB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC



INSERT INTO bank (id,amount,VERSION,user_id) VALUES (1,100,0,2)

10.启动Springboot访问http://localhost:8088/bank/2/100

Springboot2整合Mybatisplus实现乐观锁_第1张图片

点击2次同时访问

Springboot2整合Mybatisplus实现乐观锁_第2张图片

你可能感兴趣的:(springboot)