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 new file mode 100644 index 0000000000..0d55f61035 --- /dev/null +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusInterceptorTest.java @@ -0,0 +1,57 @@ +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.TestEntity; +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++) { + TestEntity test = new TestEntity(); + test.setId((long) i); + test.setName("name" + i); + testList.add(test); + } + + Exception ex = null; + try { + testMapper.updateById(testList); + testMapper.insert(testList); + testMapper.deleteBatchIds(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/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/entity/TestEntity.java b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/TestEntity.java new file mode 100644 index 0000000000..3c0d44beb5 --- /dev/null +++ b/spring-boot-starter/mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/entity/TestEntity.java @@ -0,0 +1,17 @@ +package com.baomidou.mybatisplus.test.autoconfigure.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +/** + * @author Jam804 + * @since 2024-08-22 + */ +@Data +@TableName("test") +public class TestEntity { + + 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/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..b354fa959f --- /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.TestEntity; +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..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 @@ -4,3 +4,10 @@ create table if not exists sample id bigint, name varchar ); + +drop table if exists test; +create table if not exists 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 { /**