mybatis-plus 编写批量插入,拼接values的逻辑
1.自定义Injector
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import java.util.List;
public class DeltaSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new PageJoin());
methodList.add(new InsertBatchSomeColumn()); // 指定InsertBatchSomeColumn
return methodList;
}
}
2.Injector交给spring去管理
@Bean
DeltaSqlInjector deltaSqlInjector() {
return new DeltaSqlInjector();
}
3.如果自定义了sqlSessionFactory,
额外主要GlobalConfig里要设置好刚才自定义的Injector
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
MybatisConfiguration configuration = new MybatisConfiguration();
// configuration.setLogImpl(StdOutImpl.class);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
configuration.setCallSettersOnNulls(true);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.addInterceptor(paginationInterceptor());
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setDataSource(dataSource());
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setIdType(IdType.AUTO);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setDbConfig(dbConfig);
globalConfig.setMetaObjectHandler(new MetaHandler());
globalConfig.setSqlInjector(BeanContext.getBean(DeltaSqlInjector.class)); // 这里必须得加
sqlSessionFactoryBean.setGlobalConfig(globalConfig);
sqlSessionFactoryBean.setTypeAliasesPackage("delta.dao.entity");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("delta/dao/mapper/*Mapper.xml"));
// sqlSessionFactoryBean.setTypeEnumsPackage("fama.cost.*.fama.cost.api.enums");
sqlSessionFactoryBean.setTypeEnumsPackage("delta.api.*.enums");
sqlSessionFactoryBean.setDatabaseIdProvider(databaseIdProvider());
return sqlSessionFactoryBean.getObject();
}
4.补齐基础mapper
public interface ExternalMapper<T> extends BaseMapper<T> {
/**
* 提供多表关联的表查询
* 如果提供的 {@link TableJoinParam} 包含多表信息。提供的Wrapper 的 selectColumn,where, order, 需要指定 tableName.columnName
* TableJoinParam 的工具类 {@link TableUtils}
*
* @param page
* @param param
* @param queryWrapper
* @param <E>
* @return 在 queryWrapper没有指定返回列的情况下,返回所有的字段。比如 (a.*, b.*)
*/
<E extends IPage<WideView>> E pageJoin(E page, @Param("joinParam") TableJoinParam param, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
Integer insertBatchSomeColumn(Collection<T> entityList); // 这个必须加在这
}
4.业务mapper继承这个ExternalMapper
public interface TempDataMergeMapper extends ExternalMapper<TempDataMerge> {
}
5.相关daoservice引用
@Service("tempDataMergeDaoService")
public class TempDataMergeDaoServiceImpl extends ServiceImpl<TempDataMergeMapper, TempDataMerge> implements TempDataMergeDaoService {
public void batchSave(List<TempDataMerge> merges){
List<List<TempDataMerge>> partition = Lists.partition(merges, 500);
partition.forEach(l -> {
this.baseMapper.insertBatchSomeColumn(l);
});
}
}
原创:做时间的朋友