当使用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;@SuppressWarnings("serial")public class InsertBatchSomeColumn extends AbstractMethod { private Predicate predicate; 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 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 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 getMethodList(Class> mapperClass, TableInfo tableInfo) { List methodList = super.getMethodList(mapperClass, tableInfo); methodList.add(new InsertBatchSomeColumn("insertBatchSomeColumn")); // 添加InsertBatchSomeColumn方法 return methodList; } }
来源地址:https://blog.csdn.net/xiananliu/article/details/129799731