最近做项目的时候有导入excel,excel行数超过万条,使用esayExcel解析的时候3000条导入一次数据库,使用mybatisPlus的savebatch语句,发现实际运行时候保存非常之慢,想着使用线程优化,发现还是慢;于是好奇下点开saveBatch追踪下源码,发现mybatisPlus的saveBatch竟然是这样:


这个本质和在xml写foreach没啥区别
于是开始想着查一下mybatis是不是有真正的批量,记录下mybatis批量:
1)在pom增加包:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.0</version>
</dependency>
2)在mybatis配置中增加bean:
@Bean
public EasySqlInjector easySqlInjector() {
return new EasySqlInjector();
}
3)增加injector:
public class EasySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}
4)增加easybaseMapper继承basemapper:
public interface EasyBaseMapper<T> extends BaseMapper<T> {
Integer insertBatchSomeColumn(List<T> entityList);
}
5)修改mapper,继承刚刚的easybasemapper;mybatisplus是mapper继承basemapper,我们在步骤4用esaybasemapper继承了basemapper,所以业务mapper是拥有原来的所有特性:
public interface ProvinceXiechuMapper extends EasyBaseMapper<ProvinceXiechu>
6)获取业务的list后,就可以直接调用新加的批量来实现了:
provinceXiechuMapper.insertBatchSomeColumn(insertProvinceXiechuList);
相关文章
暂无评论...
