MyBatis-plus个人学习总结 之 乐观锁

MyBatis-plus个人学习总结 之 乐观锁

一、前期准备

准备工作完全可以参考上一篇笔记MyBatis-plus快速入门https://blog.csdn.net/junR_980218/article/details/124816265
参考上面链接mybatis-plus快速入门笔记一直到测试前面部分,然后开始参考下面的操作进行今天的内容部分。

二、乐观锁操作

乐观锁:顾名思义,十分乐观,他总是认为不会出现任何问题,无论干什么都不去上锁,如果出现了问题,就再次更新值测试

1、给数据库相应表添加version字段 我这里的表是user
在这里插入图片描述
2、给实体类User添加version字段,并修改其中的set/get方法、有参构造

import com.baomidou.mybatisplus.annotation.*;
import java.util.Date;

public class User {
    
    

    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;


    private Integer age;

    private String email;

    /**
     * 字段添加填充内容
     */
    @TableField(fill= FieldFill.INSERT)
    private Date createTime;
    @TableField(fill=FieldFill.INSERT_UPDATE)
    private Date updateTime;
    /**
     * @Version 乐观锁 注解
     */
    @Version
    private Integer version;


    public Long getId() {
    
    
        return id;
    }

    public void setId(Long id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    public String getEmail() {
    
    
        return email;
    }

    public void setEmail(String email) {
    
    
        this.email = email;
    }

    public Date getCreateTime() {
    
    
        return createTime;
    }

    public void setCreateTime(Date createTime) {
    
    
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
    
    
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
    
    
        this.updateTime = updateTime;
    }

    public User() {
    
    
    }

    public User(Long id, String name, Integer age, String email, Date createTime, Date updateTime) {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }
}

3、注册组件
创建config包,并在其中编写MyBatisPlusConfig

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 加上@Configuration这个注解之后 使得他变成 能被识别到的配置类
 * @Configuration代表是一个配置类
 */
@Configuration
/**
 * 这个注解的意思是 管理事务的  m默认情况下是开启的
 */
@EnableTransactionManagement
/**
 * 可以把主启动类中的扫描包 放到这里面来
 */
@MapperScan("com.kuang.mapper")
public class MyBatisPlusConfig {
    
    
    /**
     * 注册乐观锁插件
     */
      @Bean
      public OptimisticLockerInterceptor optimisticLockerInterceptor(){
    
    
            return new OptimisticLockerInterceptor();
        }

    }

4、测试
测试类中添加如下内容

1、测试乐观锁成功的案例

   @Test
    //测试乐观锁 成功的案例
    public void testOptimisticLocker(){
    
    
        //1、查询用户信息
        User user = userMapper.selectById(1L);
        //2、修改用户信息
        user.setName("KuangShen");
        user.setEmail("[email protected]");
        //3、执行更新操作
        userMapper.updateById(user);
    }

在这里插入图片描述

2、测试乐观锁失败的案例

  @Test
    //测试乐观锁 失败的案例
    public void testOptimisticLocker1(){
    
    
        //线程1
        User user = userMapper.selectById(1L);
        user.setName("KuangShen1111");
        user.setEmail("[email protected]");

        //模拟另外一个线程执行了插队操作
        User user2 = userMapper.selectById(1L);
        user2.setName("KuangShen22222");
        user2.setEmail("[email protected]");
        userMapper.updateById(user2);

        //这里的结果是 user2更新成功了 而这个没有更新成功
        //如果没有乐观锁就会覆盖掉插队线程的值  显示线程1更新后的值
        userMapper.updateById(user);

    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/junR_980218/article/details/124845387