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 推荐一款神仙颜值的 Redis 客户端工具,开源啦文章源自JAVA秀-https://www.javaxiu.com/33985.html
文章源自JAVA秀-https://www.javaxiu.com/33985.html SpringBoot 整合 Quartz 实现 Java 定时任务的动态配置文章源自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

评论