做企业网站时需要注意哪些地方,智能手机网站模板,重庆市建设工程施工安全管理网官网,seo具体怎么优化项目基于优秀开源项目#xff1a;若依
项目背景#xff1a;项目中牵扯到数据批量导入#xff0c;为提高性能#xff0c;先考虑将MybatisPlus伪批量插入增强为真实批量插入 MybatisPlus源码#xff1a;
MybatisPlus支持批量插入#xff0c;但是跟踪源码发现底层是将批量…项目基于优秀开源项目若依
项目背景项目中牵扯到数据批量导入为提高性能先考虑将MybatisPlus伪批量插入增强为真实批量插入 MybatisPlus源码
MybatisPlus支持批量插入但是跟踪源码发现底层是将批量插入的数据循环执行了N次单条插入
IService源码 Transactional(rollbackFor {Exception.class})default boolean saveBatch(CollectionT entityList) {return this.saveBatch(entityList, 1000);}boolean saveBatch(CollectionT entityList, int batchSize);ServiceImpl源码 Transactional(rollbackFor {Exception.class})public boolean saveBatch(CollectionT entityList, int batchSize) {String sqlStatement this.getSqlStatement(SqlMethod.INSERT_ONE);return this.executeBatch(entityList, batchSize, (sqlSession, entity) - {sqlSession.insert(sqlStatement, entity);});}底层支持增强改造
1、创建自定义sql注入类MySqlInjector.java
public class MySqlInjector extends DefaultSqlInjector {public MySqlInjector() {}Overridepublic ListAbstractMethod getMethodList(Class? mapperClass, TableInfo tableInfo) {ListAbstractMethod methodList super.getMethodList(mapperClass, tableInfo);methodList.add(new InsertBatchSomeColumn(i - i.getFieldFill() ! FieldFill.UPDATE));return methodList;}}
2、创建自定义Mapper MyBaseMapper.java
public interface MyBaseMapperT extends BaseMapperT {/*** 批量插入 仅适用于mysql** param entityList 实体列表* return 影响行数*/Integer insertBatchSomeColumn(CollectionT entityList);
} 3、业务Mapper实现自定义的Mapper Repository
public interface TestMapper extends MyBaseMapperTest
{}
4、业务中批量插入使用insertBatchSomeColumn函数
insertBatchSomeColumn(list);
5、新建MyBatisPlus配置类,将自定义sql注入 MybatisPlusConfig.java
Configuration
public class MybatisPlusConfig {Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();//指定使用数据库类型interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}Beanpublic DefaultSqlInjector mySqlInjector() {return new MySqlInjector();}}
在使用默认MybatisPlus配置默认 sessionFactory时以上即可 此处基于若依需要将自定义sessionFactory设置为自定义sql注入器
配置文件位置参考
framwork模块下config包MyBatisConfig Beanpublic 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();} 基于以上大功告成。快去试试效果吧