当使用InsertBatchSomeColumn 批量插入时,字段为null时会报错,而单条插入时会使用默认值,两者行为不统一,会对开发过程产生困扰。此方法改良了原版的批量插入方法,使得批量插入和单条插入结果一致。
由于mp的接口变化较快,构造器不同版本都不一样,可能需要小改。
测试mp版本 3.5.3.1
package xxxxxxx;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import java.util.List;
import java.util.function.Predicate;
/**
* 增强版的批量插入,解决mp 中批量插入null 属性不能使用默认值的问题
*
* @see com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn
*/
@SuppressWarnings("serial")
public class InsertBatchSomeColumn extends AbstractMethod {
/**
* 字段筛选条件
*/
private Predicate<TableFieldInfo> predicate;
/**
* @param methodName 方法名
* @since 3.5.0
*/
protected InsertBatchSomeColumn(String methodName) {
super(methodName);
}
public String getMethod(SqlMethod sqlMethod) {
// 自定义 mapper 方法名
return methodName;
}
@SuppressWarnings("Duplicates")
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) +
this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) +
this.filterTableFieldInfo(fieldList, predicate, i -> getInsertSqlProperty(i,ENTITY_DOT), EMPTY);
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
String keyProperty = null;
String keyColumn = null;
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
if (tableInfo.havePK()) {
if (tableInfo.getIdType() == IdType.AUTO) {
/* 自增主键 */
keyGenerator = Jdbc3KeyGenerator.INSTANCE;
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
} else {
if (null != tableInfo.getKeySequence()) {
keyGenerator = TableInfoHelper.genKeyGenerator(getMethod(sqlMethod), tableInfo, builderAssistant);
keyProperty = tableInfo.getKeyProperty();
keyColumn = tableInfo.getKeyColumn();
}
}
}
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource, keyGenerator, keyProperty, keyColumn);
}
public InsertBatchSomeColumn setPredicate(final Predicate<TableFieldInfo> predicate) {
this.predicate = predicate;
return this;
}
private String getInsertSqlProperty(TableFieldInfo tableFieldInfo,final String prefix) {
String newPrefix = prefix == null ? "" : prefix;
String elPart = SqlScriptUtils.safeParam(newPrefix + tableFieldInfo.getEl());
//属性为空时使用默认值
String result = SqlScriptUtils.convertIf(elPart,
String.format("%s != null", newPrefix + tableFieldInfo.getEl()),false)
+ SqlScriptUtils.convertIf("default",
String.format("%s == null", newPrefix + tableFieldInfo.getEl()),false);
return result + ",";
}
}
配置方法与原批量插入相同
@Bean
public CommonSqlInjector commonSqlInjector () {
return new CommonSqlInjector();
}
public class CommonSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new InsertBatchSomeColumn("insertBatchSomeColumn")); // 添加InsertBatchSomeColumn方法
return methodList;
}
}