天天看点

springboot mybatis plus 乐观锁

mybatis plus 乐观锁

官网:https://mybatis.plus/guide/optimistic-locker-plugin.html

******************

相关注解

@Version

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Version {
}
           

支持的数据类型:int 、 Integer、long、Long、Date、Timestamp、LocalDateTime

支持的方法:updataById(id),update(Entity entity,Wrapper wrapper)

******************

示例

************

pojo 层

Student

@ApiModel(value="Student对象", description="")
public class Student extends Model<Student> {

    private static final long serialVersionUID=1L;

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

    private String name;

    private Integer age;

    @Version
    private Integer version;

    。。。。

}
           

************

controller 层

StudentController

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/save")
    public String save(){
        Student student=new Student();
        student.setName("瓜田李下");
        student.setAge(23);

        studentService.save(student);

        return "success";
    }

    @RequestMapping("/update")
    public Student update(){
        Student student=studentService.getById(1);
        student.setName("海贼王");

        studentService.updateById(student);

        return studentService.getById(student.getId());
    }
}
           

******************

插入、更新测试

springboot mybatis plus 乐观锁
springboot mybatis plus 乐观锁

继续阅读