mybatis-plus 批量插入示例
正常我们使用mybatis-plus插入的时候,首先想到的是 saveBatch 方法,不过看了下打印出来的sql和底层代码,才发现它并不是真正的批量插入。IService 中的代码为 default boolean saveBatch(Collection<T> entityList) {
return this.saveBatch(entityList, 1000);
} 实现层 ServiceImpl 中的代码为
public boolean saveBatch(Collection<T> entityList, int batchSize) {
String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {
sqlSession.insert(sqlStatement, entity);
});
}SqlMethod.INSERT_ONE 的中文枚举为<br>INSERT_ONE("insert", "插入一条数据(选择字段插入)", ""),通过监控控制台发现,它只是循环每1000条去插入,效率非常低。
参考网友的文章,找到一个支持批量操作的方法,下面直接贴上代码
1、添加批量操作参数类 CustomSqlInjector
/**
* 支持自定义SQL注入方法
*/
public class CustomSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
// 获取父类SQL注入方法列表
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// 将批量插入方法添加进去
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}2、在MybatisPlusConfig中配置
@Bean
public CustomSqlInjector customSqlInjector() {
return new CustomSqlInjector();
}3、添加自定义 Mapper接口
/**
* 自定义Mapper,添加批量插入接口
* @param <T>
*/
@Mapper
public interface CustomMapper<T> extends BaseMapper<T> {
/**
* 批量插入
* @param entityList 实体列表
* @return 影响行数
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}4、将原来的Mapper业务接口,换成继承此接口
@Mapper
public interface StudentDao extends CustomMapper<Student> {
List<Student> query(Student student);
}5、再进行测试一下
@Transactional//事务注解要加上 @Override public void testBatchCreate() { List list = new ArrayList(); int startIndex = this.lambdaQuery().select(Student::getId).count(); for (int i = 1; i
页:
[1]