项目基于优秀开源项目:若依
项目背景:项目中牵扯到数据批量导入,为提高性能,先考虑将MybatisPlus伪批量插入增强为真实批量插入
MybatisPlus支持批量插入,但是跟踪源码发现底层是将批量插入的数据循环执行了N次单条插入:
IService源码:
@Transactional(
rollbackFor = {Exception.class}
)
default boolean saveBatch(Collection entityList) {
return this.saveBatch(entityList, 1000);
}
boolean saveBatch(Collection entityList, int batchSize);
ServiceImpl源码:
@Transactional(
rollbackFor = {Exception.class}
)
public boolean saveBatch(Collection entityList, int batchSize) {
String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {
sqlSession.insert(sqlStatement, entity);
});
}
public class MySqlInjector extends DefaultSqlInjector {
public MySqlInjector() {
}
@Override
public List getMethodList(Class> mapperClass, TableInfo tableInfo) {
List methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
return methodList;
}
}
public interface MyBaseMapper extends BaseMapper {
/**
* 批量插入 仅适用于mysql
*
* @param entityList 实体列表
* @return 影响行数
*/
Integer insertBatchSomeColumn(Collection entityList);
}
@Repository
public interface TestMapper extends MyBaseMapper
{
}
insertBatchSomeColumn(list);
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//指定使用数据库类型
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public DefaultSqlInjector mySqlInjector() {
return new MySqlInjector();
}
}
在使用默认MybatisPlus配置默认 sessionFactory时,以上即可
配置文件位置,参考
framwork模块下config包MyBatisConfig
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
String mapperLocations = env.getProperty("mybatis.mapperLocations");
String configLocation = env.getProperty("mybatis.configLocation");
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
VFS.addImplClass(SpringBootVFS.class);
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
//自定义批量插入
GlobalConfig globalConfig = new GlobalConfig();
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
globalConfig.setDbConfig(dbConfig);
globalConfig.setSqlInjector(new MySqlInjector());
sessionFactory.setGlobalConfig(globalConfig);
return sessionFactory.getObject();
}
基于以上,大功告成。快去试试效果吧