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);
});
}
}
原创:做时间的朋友
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示