From 5ff40aae67f2311b108d8197e868786975ceb039 Mon Sep 17 00:00:00 2001 From: zhangyuhang12138 <2827528315@qq.com> Date: Thu, 22 Aug 2024 00:24:13 +0800 Subject: [PATCH 1/3] Commit made to supplement unit test code for bug fixes in DataChangeRecorderInnerInterceptor. --- .../MybatisPlusInterceptorTest.java | 55 +++++++++++++++++++ .../autoconfigure/MybatisPlusSampleTest.java | 2 + .../autoconfigure/entity/InterceptorTest.java | 15 +++++ .../autoconfigure/{ => entity}/Sample.java | 2 +- .../{ => mapper}/SampleMapper.java | 3 +- .../test/autoconfigure/mapper/TestMapper.java | 14 +++++ .../src/test/resources/schema.sql | 7 +++ .../test/autoconfigure/MybatisPlusTest.java | 3 + 8 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java create mode 100644 spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java rename spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/{ => entity}/Sample.java (70%) rename spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/{ => mapper}/SampleMapper.java (63%) create mode 100644 spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java new file mode 100644 index 0000000000..c589368b2b --- /dev/null +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java @@ -0,0 +1,55 @@ +package com.baomidou.mybatisplus.test.autoconfigure; + +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.DataChangeRecorderInnerInterceptor; +import com.baomidou.mybatisplus.test.autoconfigure.entity.InterceptorTest; +import com.baomidou.mybatisplus.test.autoconfigure.mapper.TestMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Jam804 + * @since 2024-08-22 + */ +//@MybatisPlusTest(properties = "spring.datasource.schema=classpath:schema.sql") +@MybatisPlusTest +public class MybatisPlusInterceptorTest { + + @Autowired + private MybatisPlusInterceptor mybatisPlusInterceptor; + + @Autowired + private TestMapper testMapper; + + @BeforeEach + public void addInterceptor() { + DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor = new DataChangeRecorderInnerInterceptor(); + mybatisPlusInterceptor.addInnerInterceptor(dataChangeRecorderInnerInterceptor); + } + + @Test + void testDataChangeRecorderInnerInterceptor() { + + List testList = new ArrayList<>(); + for(int i = 0; i < 10; i++) { + InterceptorTest test = new InterceptorTest(); + test.setId((long) i); + test.setName("name" + i); + testList.add(test); + } + + Exception ex = null; + try { + testMapper.updateById(testList); + } catch (Exception e) { + ex = e; + } + assertThat(ex).isNull(); + } +} diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusSampleTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusSampleTest.java index 5dff25b952..fd57b6eacc 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusSampleTest.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusSampleTest.java @@ -1,5 +1,7 @@ package com.baomidou.mybatisplus.test.autoconfigure; +import com.baomidou.mybatisplus.test.autoconfigure.entity.Sample; +import com.baomidou.mybatisplus.test.autoconfigure.mapper.SampleMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java new file mode 100644 index 0000000000..7df1515896 --- /dev/null +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java @@ -0,0 +1,15 @@ +package com.baomidou.mybatisplus.test.autoconfigure.entity; + +import lombok.Data; + +/** + * @author Jam804 + * @since 2024-08-22 + */ +@Data +public class InterceptorTest { + + private Long id; + + private String name; +} diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/Sample.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/Sample.java similarity index 70% rename from spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/Sample.java rename to spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/Sample.java index af48c77f76..234f1147b9 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/Sample.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/Sample.java @@ -1,4 +1,4 @@ -package com.baomidou.mybatisplus.test.autoconfigure; +package com.baomidou.mybatisplus.test.autoconfigure.entity; import lombok.Data; diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/SampleMapper.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/SampleMapper.java similarity index 63% rename from spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/SampleMapper.java rename to spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/SampleMapper.java index 152d43f6de..783f52faf3 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/SampleMapper.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/SampleMapper.java @@ -1,6 +1,7 @@ -package com.baomidou.mybatisplus.test.autoconfigure; +package com.baomidou.mybatisplus.test.autoconfigure.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.test.autoconfigure.entity.Sample; import org.apache.ibatis.annotations.Mapper; /** diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java new file mode 100644 index 0000000000..a6d422c535 --- /dev/null +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java @@ -0,0 +1,14 @@ +package com.baomidou.mybatisplus.test.autoconfigure.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.test.autoconfigure.entity.InterceptorTest; +import org.apache.ibatis.annotations.Mapper; + +/** + * @author Jam804 + * @since 2024-08-22 + */ + +@Mapper +public interface TestMapper extends BaseMapper { +} diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql index 730be2a5f8..34fca18db6 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql @@ -4,3 +4,10 @@ create table if not exists sample id bigint, name varchar ); + +drop table if exists interceptor_test; +create table if not exists interceptor_test +( + id bigint, + name varchar +); diff --git a/spring-boot-starter/mybatis-plus-spring-boot-test-autoconfigure/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTest.java b/spring-boot-starter/mybatis-plus-spring-boot-test-autoconfigure/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTest.java index f6a2120ff4..167415259c 100644 --- a/spring-boot-starter/mybatis-plus-spring-boot-test-autoconfigure/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTest.java +++ b/spring-boot-starter/mybatis-plus-spring-boot-test-autoconfigure/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTest.java @@ -15,6 +15,7 @@ */ package com.baomidou.mybatisplus.test.autoconfigure; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -24,6 +25,7 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.Import; import org.springframework.core.annotation.AliasFor; import org.springframework.core.env.Environment; import org.springframework.test.context.BootstrapWith; @@ -65,6 +67,7 @@ @AutoConfigureMybatisPlus @AutoConfigureTestDatabase @ImportAutoConfiguration +@Import(MybatisPlusInterceptor.class) public @interface MybatisPlusTest { /** From 16105c9ef33e0dc9e1213253ca9038525e59c08d Mon Sep 17 00:00:00 2001 From: zhangyuhang12138 <2827528315@qq.com> Date: Thu, 22 Aug 2024 00:30:23 +0800 Subject: [PATCH 2/3] Commit made to supplement unit test code for bug fixes in DataChangeRecorderInnerInterceptor. --- .../test/autoconfigure/MybatisPlusInterceptorTest.java | 6 +++--- .../entity/{InterceptorTest.java => TestEntity.java} | 4 +++- .../mybatisplus/test/autoconfigure/mapper/TestMapper.java | 4 ++-- .../src/test/resources/schema.sql | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) rename spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/{InterceptorTest.java => TestEntity.java} (65%) diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java index c589368b2b..46ec029bcd 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java @@ -2,7 +2,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.DataChangeRecorderInnerInterceptor; -import com.baomidou.mybatisplus.test.autoconfigure.entity.InterceptorTest; +import com.baomidou.mybatisplus.test.autoconfigure.entity.TestEntity; import com.baomidou.mybatisplus.test.autoconfigure.mapper.TestMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -36,9 +36,9 @@ public void addInterceptor() { @Test void testDataChangeRecorderInnerInterceptor() { - List testList = new ArrayList<>(); + List testList = new ArrayList<>(); for(int i = 0; i < 10; i++) { - InterceptorTest test = new InterceptorTest(); + TestEntity test = new TestEntity(); test.setId((long) i); test.setName("name" + i); testList.add(test); diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/TestEntity.java similarity index 65% rename from spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java rename to spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/TestEntity.java index 7df1515896..3c0d44beb5 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/InterceptorTest.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/TestEntity.java @@ -1,5 +1,6 @@ package com.baomidou.mybatisplus.test.autoconfigure.entity; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** @@ -7,7 +8,8 @@ * @since 2024-08-22 */ @Data -public class InterceptorTest { +@TableName("test") +public class TestEntity { private Long id; diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java index a6d422c535..b354fa959f 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/mapper/TestMapper.java @@ -1,7 +1,7 @@ package com.baomidou.mybatisplus.test.autoconfigure.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.test.autoconfigure.entity.InterceptorTest; +import com.baomidou.mybatisplus.test.autoconfigure.entity.TestEntity; import org.apache.ibatis.annotations.Mapper; /** @@ -10,5 +10,5 @@ */ @Mapper -public interface TestMapper extends BaseMapper { +public interface TestMapper extends BaseMapper { } diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql index 34fca18db6..7baa81b497 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/resources/schema.sql @@ -5,8 +5,8 @@ create table if not exists sample name varchar ); -drop table if exists interceptor_test; -create table if not exists interceptor_test +drop table if exists test; +create table if not exists test ( id bigint, name varchar From 5b05b250151c48d61b50e1ea21d60fdf59923c51 Mon Sep 17 00:00:00 2001 From: zhangyuhang12138 <2827528315@qq.com> Date: Fri, 23 Aug 2024 04:23:53 +0800 Subject: [PATCH 3/3] Commit made to resolve issues related to DataChangeRecorderInnerInterceptor and to supplement the corresponding unit tests. --- .../DataChangeRecorderInnerInterceptor.java | 65 +++++++++++-------- .../MybatisPlusInterceptorTest.java | 2 + 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/DataChangeRecorderInnerInterceptor.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/DataChangeRecorderInnerInterceptor.java index 9ed0f28dcb..792d7a876d 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/DataChangeRecorderInnerInterceptor.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/DataChangeRecorderInnerInterceptor.java @@ -134,40 +134,49 @@ public class DataChangeRecorderInnerInterceptor implements InnerInterceptor { private int BATCH_UPDATE_LIMIT = 1000; private boolean batchUpdateLimitationOpened = false; private final Map BATCH_UPDATE_LIMIT_MAP = new ConcurrentHashMap<>();//表名->批量更新上限 + private Connection connection; @Override public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { - PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); - MappedStatement ms = mpSh.mappedStatement(); - final BoundSql boundSql = mpSh.boundSql(); - SqlCommandType sct = ms.getSqlCommandType(); - if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { - PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); - OperationResult operationResult; - long startTs = System.currentTimeMillis(); - try { - Statement statement = JsqlParserGlobal.parse(mpBs.sql()); - if (statement instanceof Insert) { - operationResult = processInsert((Insert) statement, mpSh.boundSql()); - } else if (statement instanceof Update) { - operationResult = processUpdate((Update) statement, ms, boundSql, connection); - } else if (statement instanceof Delete) { - operationResult = processDelete((Delete) statement, ms, boundSql, connection); - } else { - logger.info("other operation sql={}", mpBs.sql()); + this.connection = connection; + beforeGetBoundSql(sh); + } + + @Override + public void beforeGetBoundSql(StatementHandler sh) { + if(connection != null) { + PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); + MappedStatement ms = mpSh.mappedStatement(); + final BoundSql boundSql = mpSh.boundSql(); + SqlCommandType sct = ms.getSqlCommandType(); + if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { + PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); + OperationResult operationResult; + long startTs = System.currentTimeMillis(); + try { + Statement statement = JsqlParserGlobal.parse(mpBs.sql()); + if (statement instanceof Insert) { + operationResult = processInsert((Insert) statement, mpSh.boundSql()); + } else if (statement instanceof Update) { + operationResult = processUpdate((Update) statement, ms, boundSql, connection); + } else if (statement instanceof Delete) { + operationResult = processDelete((Delete) statement, ms, boundSql, connection); + } else { + logger.info("other operation sql={}", mpBs.sql()); + return; + } + } catch (Exception e) { + if (e instanceof DataUpdateLimitationException) { + throw (DataUpdateLimitationException) e; + } + logger.error("Unexpected error for mappedStatement={}, sql={}", ms.getId(), mpBs.sql(), e); return; } - } catch (Exception e) { - if (e instanceof DataUpdateLimitationException) { - throw (DataUpdateLimitationException) e; + long costThis = System.currentTimeMillis() - startTs; + if (operationResult != null) { + operationResult.setCost(costThis); + dealOperationResult(operationResult); } - logger.error("Unexpected error for mappedStatement={}, sql={}", ms.getId(), mpBs.sql(), e); - return; - } - long costThis = System.currentTimeMillis() - startTs; - if (operationResult != null) { - operationResult.setCost(costThis); - dealOperationResult(operationResult); } } } diff --git a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java index 46ec029bcd..0d55f61035 100644 --- a/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java @@ -47,6 +47,8 @@ void testDataChangeRecorderInnerInterceptor() { Exception ex = null; try { testMapper.updateById(testList); + testMapper.insert(testList); + testMapper.deleteBatchIds(testList); } catch (Exception e) { ex = e; }