mysql支持一条sql语句插入多条数据。但是Mybatis-Plus中默认提供的saveBatch、updateBatchById方法并不能算是真正的批量语句,而是遍历实体集合执行INSERT_ONE、UPDATE_BY_ID语句。
mybatis-plus虽然做了分批请求、一次提交的处理。但如果jdbc不启用配置rewriteBatchedStatements,那么批量提交的sql到了mysql就还是一条一条执行,mysql并不会将这些sql重写为insert多值插入,相比一条sql批量插入,性能上会差点。
rewriteBatchedStatements文档
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-connp-props-performance-extensions.html#cj-conn-prop_rewriteBatchedStatements

##sql格式(不止这些字段)
##sql格式(不止这些字段)
insert into user (name, age) values ("张三", 17), ("李四", 18);
##sql格式(不止这些字段)
##sql格式(不止这些字段)
update
user
set
name=
(
case id when 1 then '张三'
when 2 then '李四'
else name
end),
age=
(
case id when 1 then 16
when 2 then 26
else age
end)
where
id in ( 1 , 2 )
综合比较效率
| 10000条数据 | mybatis-plus | 自定义sql |
|---|---|---|
| 批量插入 | 3.6s | 1.8s |
| 批量更新 | 3.1s | 5.8s |

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public class InsertBatchMethod extends AbstractMethod {
/**
* insert into user(id, name, age) values (2, "b", 24), (3, "c", 26);
*/
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
final String sql = "";
final String fieldSql = prepareFieldSql(tableInfo);
final String valueSql = prepareValuesSql(tableInfo);
final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
SqlSource sqlSource = languageDriver.createSqlSource(
configuration, sqlResult, modelClass);
//加入Configuration中的Map mappedStatements,
//这个mappedStatements保存我们xml中写的各种标签
public class UpdateBatchMethod extends AbstractMethod {
/**
update user set
name=
(CASE
id WHEN 1 THEN '张三'
WHEN 2 THEN '李四'
end),
age =
(CASE
id WHEN 1 THEN '2'
WHEN 2 THEN '2'
end) where id in (1,2);
*/
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
final String sql = "";
final String valueSql = prepareValuesSql(tableInfo);
final String sqlResult = String.format(sql, tableInfo.getTableName(), valueSql);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
}
private String prepareValuesSql(TableInfo tableInfo) {
final StringBuilder valueSql = new StringBuilder();
valueSql.append("\n");
tableInfo.getFieldList().forEach(x -> {
valueSql.append("\n");
valueSql.append("\n");
valueSql.append("\n");
valueSql.append("when #{item.id} then #{item.").append(x.getProperty()).append("}\n");
valueSql.append(" \n");
valueSql.append(" \n");
valueSql.append("else ").append(x.getColumn());
valueSql.append(" \n");
});
valueSql.append(" \n");
return valueSql.toString();
}
}
import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn; import org.springframework.context.annotation.Configuration; import java.util.List; /** * 自定义方法SQL注入器 */ @Configuration public class CustomizedSqlInjector extends DefaultSqlInjector { @Override public ListgetMethodList(Class> mapperClass) { List methodList = super.getMethodList(mapperClass); methodList.add(new InsertBatchSomeColumn()); methodList.add(new InsertBatchMethod()); methodList.add(new UpdateBatchMethod()); return methodList; } }
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * 业务实体基础Mapper接口 */ public interface BaseEntityMapperextends BaseMapper { int insertBatch(@Param("list") List list); Integer insertBatchSomeColumn(List list); int updateBatch(@Param("list") List list); }
import com.test.demo.model.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseEntityMapper{ }
... userMapper.insertBatch(userList); ...
上一篇:Spring Task 超详解版