MyBatis 三种批量插入方式的比较,我推荐第3个!

沙海 2021年6月21日12:52:46Java评论23字数 1500阅读5分0秒阅读模式
摘要

MyBatis 三种批量插入方式的比较,我推荐第3个! 架构师专栏

MyBatis 三种批量插入方式的比较,我推荐第3个!

架构师专栏 文章源自JAVA秀-https://www.javaxiu.com/33985.html

大家好,我是磊哥。文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

数据库使用的是SQLServer,JDK版本1.8,运行在SpringBoot环境下 对比3种可用的方式文章源自JAVA秀-https://www.javaxiu.com/33985.html

  • 反复执行单条插入语句文章源自JAVA秀-https://www.javaxiu.com/33985.html

  • xml拼接sql文章源自JAVA秀-https://www.javaxiu.com/33985.html

  • 批处理执行文章源自JAVA秀-https://www.javaxiu.com/33985.html

先说结论:少量插入请使用反复插入单条数据,方便。数量较多请使用批处理方式。(可以考虑以有需求的插入数据量20条左右为界吧,在我的测试和数据库环境下耗时都是百毫秒级的,方便最重要)。文章源自JAVA秀-https://www.javaxiu.com/33985.html

无论何时都不用xml拼接sql的方式。文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

注 意文章源自JAVA秀-https://www.javaxiu.com/33985.html

 文末有:3625页互联网大厂面试题 文章源自JAVA秀-https://www.javaxiu.com/33985.html

代码

拼接SQL的xml

newId()是sqlserver生成UUID的函数,与本文内容无关文章源自JAVA秀-https://www.javaxiu.com/33985.html

<insert id="insertByBatch" parameterType="java.util.List">    INSERT INTO tb_item VALUES    <foreach collection="list" item="item" index="index" separator=",">        (newId(),#{item.uniqueCode},#{item.projectId},#{item.name},#{item.type},#{item.packageUnique},        #{item.isPackage},#{item.factoryId},#{item.projectName},#{item.spec},#{item.length},#{item.weight},        #{item.material},#{item.setupPosition},#{item.areaPosition},#{item.bottomHeight},#{item.topHeight},        #{item.serialNumber},#{item.createTime}</foreach></insert>Mapper接口Mapper 是 mybatis插件tk.Mapper 的接口,与本文内容关系不大public interface ItemMapper extends Mapper<Item> {    int insertByBatch(List<Item> itemList);}

Service类文章源自JAVA秀-https://www.javaxiu.com/33985.html

@Servicepublic class ItemService {    @Autowired    private ItemMapper itemMapper;    @Autowired    private SqlSessionFactory sqlSessionFactory;    //批处理    @Transactional

文章源自JAVA秀-https://www.javaxiu.com/33985.html

近期技术热文文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

自从在 IDEA 中用了热部署神器 JRebel,开发效率提升了 10 倍!文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

推荐一款神仙颜值的 Redis 客户端工具,开源啦文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

SpringBoot 整合 Quartz 实现 Java 定时任务的动态配置文章源自JAVA秀-https://www.javaxiu.com/33985.html

文章源自JAVA秀-https://www.javaxiu.com/33985.html

第3版:互联网大厂面试题文章源自JAVA秀-https://www.javaxiu.com/33985.html

包括 Java 集合、JVM、多线程、并发编程、设计模式、算法调优、Spring全家桶、Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、MongoDB、Redis、MySQL、RabbitMQ、Kafka、Linux、Netty、Tomcat、Python、HTML、CSS、Vue、React、JavaScript、Android 大数据、阿里巴巴等大厂面试题等、等技术栈!文章源自JAVA秀-https://www.javaxiu.com/33985.html

阅读原文: 高清 7701页大厂面试题  PDF文章源自JAVA秀-https://www.javaxiu.com/33985.html

阅读原文文章源自JAVA秀-https://www.javaxiu.com/33985.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定