Pārlūkot izejas kodu

mybatis plus 乐观锁测试

zhang 2 gadi atpakaļ
vecāks
revīzija
14c7207d34

+ 6 - 0
src/main/java/com/api/config/MybatisPlusConfig.java

@@ -10,6 +10,7 @@ package com.api.config;
10 10
 
11 11
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
12 12
 import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
13
+import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
13 14
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
14 15
 import org.springframework.context.annotation.Bean;
15 16
 import org.springframework.context.annotation.Configuration;
@@ -30,6 +31,11 @@ public class MybatisPlusConfig {
30 31
         return new PaginationInterceptor();
31 32
     }
32 33
 
34
+    @Bean
35
+    public OptimisticLockerInterceptor getOpt() {
36
+        return new OptimisticLockerInterceptor();
37
+    }
38
+
33 39
     @Bean
34 40
     public ISqlInjector sqlInjector() {
35 41
         return new LogicSqlInjector();

+ 9 - 0
src/main/java/com/api/dao/ProductMapper.java

@@ -0,0 +1,9 @@
1
+package com.api.dao;
2
+
3
+import com.api.entity.ProductEntity;
4
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+@Mapper
8
+public interface ProductMapper extends BaseMapper<ProductEntity> {
9
+}

+ 19 - 0
src/main/java/com/api/entity/ProductEntity.java

@@ -0,0 +1,19 @@
1
+package com.api.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.TableId;
4
+import com.baomidou.mybatisplus.annotation.TableName;
5
+import com.baomidou.mybatisplus.annotation.Version;
6
+import lombok.Data;
7
+
8
+@Data
9
+@TableName("t_product")
10
+public class ProductEntity {
11
+    @TableId
12
+    private Integer id;
13
+
14
+    private String productName;
15
+    private Integer productPrice;
16
+    @Version
17
+    private Integer version;
18
+
19
+}

+ 27 - 0
src/main/test/java/com/api/MybatisPlusTest2.java

@@ -0,0 +1,27 @@
1
+package com.api;
2
+
3
+import com.api.dao.ProductMapper;
4
+import com.api.entity.ProductEntity;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.boot.test.context.SpringBootTest;
9
+import org.springframework.test.context.junit4.SpringRunner;
10
+
11
+@RunWith(SpringRunner.class)
12
+@SpringBootTest
13
+public class MybatisPlusTest2 {
14
+    @Autowired
15
+    private ProductMapper productMapper;
16
+    // 测试乐观锁
17
+    @Test
18
+    public void testOpt() {
19
+        ProductEntity productZhang = productMapper.selectById(0);
20
+        ProductEntity productWang = productMapper.selectById(0);
21
+        productZhang.setProductPrice(productZhang.getProductPrice()+1);
22
+        int updateZhang = productMapper.updateById(productZhang);
23
+        productWang.setProductPrice(productWang.getProductPrice()-11);
24
+        int updateWang = productMapper.updateById(productWang);
25
+
26
+    }
27
+}