mybatis-plus 注解,mybatisplus注解sql

  mybatis-plus 注解,mybatisplus注解sql

  00-1010 1.简介2。注释3。什么是乐观锁定3.1乐观锁定简介3.2乐观锁定示例4。示例代码

  00-1010大家好。今天我想和大家分享一些关于Mybatis-plus的服务层的方法。今天没有总结,因为都是API。没什么好总结的。直接看通话就行了。

  我们将介绍@Version注释的用法,以及每个属性的实际意义和用法。

  00-1010在MyBatis Plus中,乐观锁定是用@Version实现的,这个注释用在字段上。

  

目录

 

  00-1010乐观锁相对悲观锁。乐观锁定假设数据在一般情况下不会引起冲突,所以当数据被提交和更新时,数据冲突会被正式检测到。如果发现冲突,将向用户返回错误信息,以便用户可以决定如何进行乐观锁定。适用于读操作多的场景,可以提高程序的吞吐量。

  00-1010有两个线程A和B,分别从数据库读取数据。执行后,线程A和线程B的版本都等于1。如下图

  线程A处理业务并提交数据。此时,数据库中该记录的版本是2。如下图:

  线程B也完成了事务处理并提交了数据。此时,数据库中的版本已经等于2,而线程的版本仍然是1。程序给出错误信息,不允许线程B操作数据。如下图:

  乐观锁定采用了更宽松的锁定机制。乐观锁是一种相对悲观的锁机制,也是一种避免误读数据库和业务处理时间过长导致数据处理错误的机制。但乐观锁不会刻意使用数据库本身的锁机制,而是会根据数据本身来保证数据的正确性。

  00-1010这个例子将在前面使用的用户表上执行。继续之前,现在将版本字段添加到用户表中。

  alter table ` user ` add column ` version ` int unsigned null comment 版本信息;info

  用以下代码定义用户表的JavaBean:

  导入com . baomi dou . mybatisplus . annotation . *;@TableName(value=user )公共类annotation user 5 bean { @ TableId(value= user _ id ,type=IdType。AUTO)私有字符串userId@TableField(name )私有字符串名称;@TableField(sex )私串sex;@TableField(age )私有整数年龄;@Version private int版本;//忽略getter和setter方法}添加MyBatis Plus的乐观锁定插件,它会自动帮我们加一个版本。

  注意,这里和分页操作一样,需要进行配置。如果没有配置,@Version不会生效。

  导入com . baomi dou . mybatisplus . annotation . dbtype;导入com . baomi dou . mybatisplus . extension . plugins . mybatisplusinterceptor;导入com . baomi dou . mybatisplus . extension . plugins . inner . optimisticlockerinnerinterceptor;导入org . spring framework . context . annotation . bean;导入org . spring framework . context . annotation . configuration;@Configurationpublic类MybatisPlusConfig { @ Bean public MybatisPlusInterceptor pagination interceptor(){

   MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 乐观锁插件 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor; } }测试乐观锁代码,我们创建两个线程 A 和 B 分别去修改用户ID为 1 的用户年龄,然后观察年龄和version字段的值

  

package com.hxstrive.mybatis_plus.simple_mapper.annotation; import com.hxstrive.mybatis_plus.mapper.AnnotationUser5Mapper;import com.hxstrive.mybatis_plus.model.AnnotationUser5Bean;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.concurrent.CountDownLatch; @RunWith(SpringRunner.class)@SpringBootTestclass AnnotationDemo5 { @Autowired private AnnotationUser5Mapper userMapper; @Test void contextLoads() throws Exception { // 重置数据 AnnotationUser5Bean user5Bean = new AnnotationUser5Bean(); user5Bean.setUserId(1); user5Bean.setAge(0); user5Bean.setVersion(0); userMapper.updateById(user5Bean); // 修改数据 for (int i = 0; i < 10; i++) { System.out.println("第 " + (i + 1) + " 次修改数据"); final CountDownLatch countDownLatch = new CountDownLatch(2); modifyUser(countDownLatch, "My-Thread-A", 1); modifyUser(countDownLatch, "My-Thread-B", 1); countDownLatch.await(); Thread.sleep(100L); } } private void modifyUser(final CountDownLatch countDownLatch, String threadName, int userId) { Thread t = new Thread(new Runnable() { @Override public void run() { try { String threadName = Thread.currentThread().getName(); try { AnnotationUser5Bean userBean = userMapper.selectById(userId); if (null == userBean) { return; } AnnotationUser5Bean newBean = new AnnotationUser5Bean(); newBean.setName(userBean.getName()); newBean.setSex(userBean.getSex()); newBean.setAge(userBean.getAge() + 1); newBean.setUserId(userBean.getUserId()); newBean.setVersion(userBean.getVersion()); int result = userMapper.updateById(newBean); System.out.println("result=" + result + " ==> " + userBean); } catch (Exception e) { System.err.println(threadName + " " + e.getMessage()); } } finally { countDownLatch.countDown(); } } }); t.setName(threadName); t.start(); } }

在运行上面代码之前,我们数据库中的记录值如下:

 

  user_idnamesexageversion1测试男00运行上面程序,数据库记录如下:

  user_idnamesexageversion1测试男0161.上面代码将执行10次循环操作,每次操作启动两个线程(线程 A 和 线程 B)去修改用户数据。

  2.如果数据没有任何冲突,则用户的年龄应该是 20。但是上面程序运行完成后年龄为 16

  3.这就说明,在线程运行的时候,可能A 刚好修改了version,并没有执行完,就到B线程了,就导致B线程修改失败

  以上就是详解MybatisPlus中@Version注解的使用的详细内容,更多关于MybatisPlus @Version注解的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: