Skip to content

Commit

Permalink
新增参数填充器跳过方式(基于MappedStatement#id).
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Mar 31, 2024
1 parent 6782dff commit cd02388
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public class MybatisParameterHandler extends DefaultParameterHandler {
private final Object parameterObject;
private final Configuration configuration;
private final SqlCommandType sqlCommandType;
private final MappedStatement mappedStatement;

public MybatisParameterHandler(MappedStatement mappedStatement, Object parameter, BoundSql boundSql) {
super(mappedStatement, parameter, boundSql);
this.mappedStatement = mappedStatement;
this.configuration = mappedStatement.getConfiguration();
this.sqlCommandType = mappedStatement.getSqlCommandType();
this.parameterObject = processParameter(parameter);
Expand Down Expand Up @@ -158,15 +160,15 @@ protected void populateKeys(TableInfo tableInfo, MetaObject metaObject, Object e

protected void insertFill(MetaObject metaObject, TableInfo tableInfo) {
GlobalConfigUtils.getMetaObjectHandler(this.configuration).ifPresent(metaObjectHandler -> {
if (metaObjectHandler.openInsertFill() && tableInfo.isWithInsertFill()) {
if (metaObjectHandler.openInsertFill() && metaObjectHandler.openInsertFill(mappedStatement) && tableInfo.isWithInsertFill()) {
metaObjectHandler.insertFill(metaObject);
}
});
}

protected void updateFill(MetaObject metaObject, TableInfo tableInfo) {
GlobalConfigUtils.getMetaObjectHandler(this.configuration).ifPresent(metaObjectHandler -> {
if (metaObjectHandler.openUpdateFill() && tableInfo.isWithUpdateFill()) {
if (metaObjectHandler.openUpdateFill() && metaObjectHandler.openUpdateFill(mappedStatement) && tableInfo.isWithUpdateFill()) {
metaObjectHandler.updateFill(metaObject);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Collections;
Expand All @@ -36,18 +37,46 @@ public interface MetaObjectHandler {

/**
* 是否开启了插入填充
*
* @deprecated 3.5.6 {@link #openInsertFill(MappedStatement)}
*/
@Deprecated
default boolean openInsertFill() {
return true;
}

/**
* 是否开启插入填充
*
* @param mappedStatement {@link MappedStatement}
* @return 是否开启
* @since 3.5.6
*/
default boolean openInsertFill(MappedStatement mappedStatement) {
return true;
}

/**
* 是否开启了更新填充
*
* @deprecated 3.5.6 {@link #openUpdateFill(MappedStatement)}
*/
@Deprecated
default boolean openUpdateFill() {
return true;
}

/**
* 是否开启了更新填充
*
* @param mappedStatement {@link MappedStatement}
* @return 是否开启
* @since 3.5.6
*/
default boolean openUpdateFill(MappedStatement mappedStatement) {
return true;
}

/**
* 插入元对象字段填充(用于插入时对公共字段的填充)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,63 @@ public void updateFill(MetaObject metaObject) {

}

@Test
void testIgnoreMapperStatement() {
MappedStatement mappedStatement;
Configuration configuration = new MybatisConfiguration();
BoundSql boundSql = mock(BoundSql.class);
StaticSqlSource staticSqlSource = mock(StaticSqlSource.class);
GlobalConfigUtils.getGlobalConfig(configuration).setIdentifierGenerator(DefaultIdentifierGenerator.getInstance()).setMetaObjectHandler(new MetaObjectHandler() {

@Override
public boolean openInsertFill(MappedStatement mappedStatement) {
//根据msId跳过填充
return !"com.baomidou.demo.ignoreFillSave".equals(mappedStatement.getId());
}

@Override
public boolean openUpdateFill(MappedStatement mappedStatement) {
//根据msId跳过填充
return !"com.baomidou.demo.ignoreFillUpdate".equals(mappedStatement.getId());
}

@Override
public void insertFill(MetaObject metaObject) {
setFieldValByName("id", 666L, metaObject);
setFieldValByName("insertOperator", "秋秋", metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
setFieldValByName("updateOperator", "秋秋", metaObject);
}

});
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), Model.class);
Model model;

model = new Model("测试新增忽略填充");
mappedStatement = new MappedStatement.Builder(configuration, "com.baomidou.demo.ignoreFillSave", staticSqlSource, SqlCommandType.INSERT).build();
new MybatisParameterHandler(mappedStatement, model, boundSql);
assertThat(model.getId()).isNull();
assertThat(model.getInsertOperator()).isNull();

model = new Model("测试新增填充");
mappedStatement = new MappedStatement.Builder(configuration, "com.baomidou.demo.save", staticSqlSource, SqlCommandType.INSERT).build();
new MybatisParameterHandler(mappedStatement, model, boundSql);
assertThat(model.getId()).isNotNull();
assertThat(model.getInsertOperator()).isNotNull();
assertThat(model.getInsertOperator()).isEqualTo("秋秋");

model = new Model("测试更新忽略填充");
mappedStatement = new MappedStatement.Builder(configuration, "com.baomidou.demo.ignoreFillUpdate", staticSqlSource, SqlCommandType.UPDATE).build();
new MybatisParameterHandler(mappedStatement, model, boundSql);
assertThat(model.getUpdateOperator()).isNull();

model = new Model("测试更新填充");
mappedStatement = new MappedStatement.Builder(configuration, "com.baomidou.demo.update", staticSqlSource, SqlCommandType.UPDATE).build();
new MybatisParameterHandler(mappedStatement, model, boundSql);
assertThat(model.getUpdateOperator()).isEqualTo("秋秋");
}

}

0 comments on commit cd02388

Please sign in to comment.