import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 继承BaseMapper,扩展自定义批量插入方法
*/
public interface BaseMapperExt<T> extends BaseMapper<T> {
/**
* 批量插入
*
* @param list
* @return
*/
int insertBatch(@Param("list") List<T> list);
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 注入
*/
@Configuration
public class MybatisPlusExtConfig {
/**
* 自定义批量插入 SQL 注入器
*/
@Bean
public UgBatchSqlInjector ugBatchSqlInjector() {
return new UgBatchSqlInjector();
}
}
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.util.List;
/**
* 批量
*/
public class UgBatchSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
// super.getMethodList() 保留 Mybatis Plus 自带的方法
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// 添加自定义方法:批量插入,方法名为 insertBatchSomeColumn
methodList.add(new InsertBatch());
return methodList;
}
}
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.Constants;
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;
/**
* 批量插入
*/
public class InsertBatch extends AbstractMethod {
/**
* 字段筛选条件
*/
private Predicate<TableFieldInfo> predicate;
private static final String METHOD_NAME = "insertBatch";
@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, this::getInsertSqlProperty, 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(METHOD_NAME, 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, METHOD_NAME, sqlSource, keyGenerator, keyProperty, keyColumn);
}
/**
* 设置字段筛选条件:过滤掉没有值的字段
*
* @param predicate 字段筛选条件
* @return this
*/
public InsertBatch setPredicate(final Predicate<TableFieldInfo> predicate) {
this.predicate = predicate;
return this;
}
/**
* 辅助方法,用于获取插入 SQL 语句中的属性值部分
*
* @param tableFieldInfo 表字段信息
* @return 插入 SQL 语句中的属性值部分
*/
private String getInsertSqlProperty(TableFieldInfo tableFieldInfo) {
String newPrefix = Constants.ENTITY_DOT;
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 + ",";
}
}