update 重写 InsertAll 方法实现 可完美替代 saveBatch 秒级插入上万数据
This commit is contained in:
parent
1cd64a1c4e
commit
8ffe111086
@ -14,8 +14,7 @@ import java.util.Collection;
|
|||||||
public interface BaseMapperPlus<T> extends BaseMapper<T> {
|
public interface BaseMapperPlus<T> extends BaseMapper<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单sql批量插入( 全量填充 无视数据库默认值 )
|
* 单sql批量插入( 全量填充 )
|
||||||
* 适用于无脑插入
|
|
||||||
*/
|
*/
|
||||||
int insertAll(@Param("list") Collection<T> batchList);
|
int insertAll(@Param("list") Collection<T> batchList);
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ public class ServicePlusImpl<M extends BaseMapperPlus<T>, T, K> extends ServiceI
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单sql批量插入( 全量填充 无视数据库默认值 )
|
* 单sql批量插入( 全量填充 )
|
||||||
* 适用于无脑插入
|
* 适用于无脑插入
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -2,6 +2,7 @@ package com.ruoyi.common.core.mybatisplus.methods;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
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.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
@ -11,13 +12,17 @@ import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
|||||||
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
import org.apache.ibatis.mapping.SqlSource;
|
import org.apache.ibatis.mapping.SqlSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单sql批量插入( 全量填充 无视数据库默认值 )
|
* 单sql批量插入( 全量填充 )
|
||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
public class InsertAll extends AbstractMethod {
|
public class InsertAll extends AbstractMethod {
|
||||||
|
|
||||||
|
private final static String[] FILL_PROPERTY = {"createTime", "createBy", "updateTime", "updateBy"};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
final String sql = "<script>insert into %s %s values %s</script>";
|
final String sql = "<script>insert into %s %s values %s</script>";
|
||||||
@ -63,10 +68,32 @@ public class InsertAll extends AbstractMethod {
|
|||||||
final StringBuilder valueSql = new StringBuilder();
|
final StringBuilder valueSql = new StringBuilder();
|
||||||
valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
|
valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
|
||||||
if (StringUtils.isNotBlank(tableInfo.getKeyColumn())) {
|
if (StringUtils.isNotBlank(tableInfo.getKeyColumn())) {
|
||||||
valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
|
valueSql.append("\n#{item.").append(tableInfo.getKeyProperty()).append("},\n");
|
||||||
|
}
|
||||||
|
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
|
||||||
|
int last = fieldList.size() - 1;
|
||||||
|
for (int i = 0; i < fieldList.size(); i++) {
|
||||||
|
String property = fieldList.get(i).getProperty();
|
||||||
|
if (!StringUtils.equalsAny(property, FILL_PROPERTY)) {
|
||||||
|
valueSql.append("<if test=\"item.").append(property).append(" != null\">");
|
||||||
|
valueSql.append("#{item.").append(property).append("}");
|
||||||
|
if (i != last) {
|
||||||
|
valueSql.append(",");
|
||||||
|
}
|
||||||
|
valueSql.append("</if>");
|
||||||
|
valueSql.append("<if test=\"item.").append(property).append(" == null\">");
|
||||||
|
valueSql.append("DEFAULT");
|
||||||
|
if (i != last) {
|
||||||
|
valueSql.append(",");
|
||||||
|
}
|
||||||
|
valueSql.append("</if>");
|
||||||
|
} else {
|
||||||
|
valueSql.append("#{item.").append(property).append("}");
|
||||||
|
if (i != last) {
|
||||||
|
valueSql.append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
|
|
||||||
valueSql.delete(valueSql.length() - 1, valueSql.length());
|
|
||||||
valueSql.append("</foreach>");
|
valueSql.append("</foreach>");
|
||||||
return valueSql.toString();
|
return valueSql.toString();
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ public class TestBatchController extends BaseController {
|
|||||||
private final ITestDemoService iTestDemoService;
|
private final ITestDemoService iTestDemoService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增批量方法 ( 全量覆盖填充 )
|
* 新增批量方法 可完美替代 saveBatch 秒级插入上万数据 (对mysql负荷较大)
|
||||||
*/
|
*/
|
||||||
@ApiOperation(value = "新增批量方法")
|
@ApiOperation(value = "新增批量方法")
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user