文章目录
- 现状
- 优化
- 效果
- 报错
现状
一般来说,批量插入可以使用 MyBatisPlus 中 ServiceImpl 自带的方法 saveBatch
打开 sql 日志,application.yml 添加配置,mapper-locations 配置 mapper 路径
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志 mapper-locations: classpath*:mapper/**/*Mapper.xml
可以发现插入是在同一个 SqlSession,但并不是理想中的批量插入
它的插入算法我没有细究,但从日志观察可以看出它的插入条数是无序的,如果可以一次插入全部,效率应该更高
优化
MyBatisPlus 预留了 insertBatchSomeColumn 方法,可以实现批量插入,下面介绍一下如何配置
- MyBatisPlus 依赖
com.baomidou mybatis-plus-boot-starter 3.5.2 - 新建 Sql 注射器 BatchSqlInjector
import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn; import java.util.List; public class BatchSqlInjector extends DefaultSqlInjector { @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; } } - MybatisPlusConfig 配置 BatchSqlInjector Bean,可忽略这里配置的分页插件
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @Configuration public class MybatisPlusConfig { /** * 分页插件 * * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); pageInterceptor.setMaxLimit(500L); pageInterceptor.setOptimizeJoin(true); interceptor.addInnerInterceptor(pageInterceptor); return interceptor; } /** * 批量插入 * * @return */ @Bean public BatchSqlInjector easySqlInjector() { return new BatchSqlInjector(); } }
- 配置 BatchBaseMapper 继承 BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.Collection; public interface BatchBaseMapper
extends BaseMapper { /** * 批量插入 仅适用于mysql * * @param entityList 实体列表 * @return 影响行数 */ Integer insertBatchSomeColumn(Collection entityList); } - 业务 Mapper 继承 BatchBaseMapper
@Repository public interface ISapCustomerMapper extends BatchBaseMapper
{ } - service 创建 createBatch 作为新的批量插入方法
public class SapCustomerServiceImpl extends ServiceImpl
{ void createBatch(List entityList) { if(!entityList.isEmpty()){ baseMapper.insertBatchSomeColumn(entityList); } } } 注意:不建议直接用 mapper 的 insertBatchSomeColumn 方法,因为当 entityList 为空时会报错
其实就是 INSERT INTO 表名(字段1,字段2,字段3) VALUES 后面为空
NestedRuntimeException:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1
效果
3600 条数据
优化前:2058 毫秒
优化后:1293 毫秒
15000 条数据
优化前:8958 毫秒
优化后:2037 毫秒
可以看出,数据越多,优化效果越明细
通过这次测试发现,打开 sql 日志后,会明细拖慢 sql 执行效率,数据越多越明细
报错
Packet for query is too large (82,807,536 > 67,108,864). You can change this value on the server by setting the max_allowed_packet’ variable
原因: 一次插入数量太多的数据,超出了 mysql 默认设置
解决办法: 在 service 层限制插入数量
static int batchSize = 10000; public void createBatch(List
entityList) { if (!entityList.isEmpty()) { int size = entityList.size(); int idxLimit = Math.min(batchSize, size); int i = 1; List oneBatchList = new ArrayList<>(); for (Iterator var7 = entityList.iterator(); var7.hasNext(); ++i) { TestPOelement = var7.next(); oneBatchList.add(element); if (i == idxLimit) { baseMapper.insertBatchSomeColumn(oneBatchList); oneBatchList.clear(); idxLimit = Math.min(idxLimit + batchSize, size); } } } }
猜你喜欢
- 10天前(杭州西湖区万怡酒店正式开业了吗)杭州西湖区万怡酒店正式开业
- 10天前(云南滇陇工程咨询有限公司)陇滇携手谋发展 文旅合作谱新篇
- 10天前(澳涞山庄见证北欧零碳到中国实践,世界十佳环境保护城市榜单发布)澳涞山庄见证北欧零碳到中国实践,世界十佳环境保护城市榜单发布
- 10天前(夏日纵享 邂逅双面姑苏是哪一集)夏日纵享 邂逅双面姑苏
- 10天前(岭南东方大酒店)粤西成势 | 阳江阳春长兴岭南东方酒店正式签约,粤西文旅再添明珠
- 10天前(世茂海峡大厦多高)巴西地产高管齐聚厦门世茂海峡大厦 共探超高层建筑锻造经验
- 10天前(辽宁新增6个国家4a级旅游景区有哪些)辽宁新增6个国家4A级旅游景区
- 10天前(大黄山景区高质量发展联盟成立多少年)大黄山景区高质量发展联盟成立
- 10天前(殷建祥简历)全国十大牛商解码:殷建祥如何用178天技术突围打造星空梦星空房
- 10天前(“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%)“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章