Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix #6428

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open

Bug fix #6428

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,40 +134,49 @@ public class DataChangeRecorderInnerInterceptor implements InnerInterceptor {
private int BATCH_UPDATE_LIMIT = 1000;
private boolean batchUpdateLimitationOpened = false;
private final Map<String, Integer> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TestEntity> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baomidou.mybatisplus.test.autoconfigure;
package com.baomidou.mybatisplus.test.autoconfigure.entity;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TestEntity> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -65,6 +67,7 @@
@AutoConfigureMybatisPlus
@AutoConfigureTestDatabase
@ImportAutoConfiguration
@Import(MybatisPlusInterceptor.class)
public @interface MybatisPlusTest {

/**
Expand Down
Loading