mybatis批量新增数据

数据量大的时候如果在循环中执行单条新增操作,是非常慢的。那么如何在mybatis中实现批量新增数据呢?

方法

insert 标签的 foreach 属性可以用于批量插入数据。您可以使用 foreach 属性遍历一个集合,并为集合中的每个元素生成一条插入语句。

bash 复制代码
<insert id="insertBatch" parameterType="list">
  insert into table_name (column1, column2)
  values
  <foreach collection="list" item="item" separator=",">
    (#{item.column1}, #{item.column2})
  </foreach>
</insert>

实战

表结构

我有一张表结构如下,为了好理解我把结构做了适当的精简:

sql 复制代码
create table exa_paper_question
(
    paper_id       bigint          not null comment '试卷编号',
    question_id    bigint          not null comment '试题编号'
)
    comment '试卷和试题关联表';

实体

java 复制代码
public class ExaPaperQuestion
{
    private static final long serialVersionUID = 1L;
    /** 试卷编号 */
    private Long paperId;
    /** 试题编号 */
    private Long questionId;
    // getter setter 略
 }

maper.java

java 复制代码
 /**
     * 批量新增试卷与试题的关系
     *
     * @param list 试卷和试题关联集合
     */
    public int batchAddQuestionToPage(List<ExaPaperQuestion> list);

mapper.xml

xml 复制代码
  <!--批量新增试卷与试题的关系 -->
    <insert id="batchAddQuestionToPage" parameterType="list">
        insert into exa_paper_question (paper_id, question_id)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.paperId}, #{item.questionId})
        </foreach>
    </insert>

业务调用处示例

java 复制代码
 List<ExaPaperQuestion> list = new ArrayList<>();
for(Long id:questionIds){
      ExaPaperQuestion pq = new ExaPaperQuestion();
      pq.setPaperId(request.getPaperId());
      pq.setQuestionId(id);
      list.add(pq);
  }

  exaPaperQuestionMapper.batchAddQuestionToPage(list);

批量删除

上面是演示的批量新增,那就也能用到批量删除。

如果是有主键的表,批量删除格式如下:

xml 复制代码
  <delete id="deleteExaQuestionByQuestionIds" parameterType="String">
        delete from exa_question where question_id in
        <foreach item="questionId" collection="array" open="(" separator="," close=")">
            #{questionId}
        </foreach>
    </delete>

如果是我示例中的这张表,是需要两个条件确实一条记录的,那么就这样写:

xml 复制代码
   <!--批量删除试卷与试题的关系 -->
    <insert id="batchDeleteExaPaperQuestionByPaperId" parameterType="list">
        delete from exa_paper_question
        where (paper_id, question_id) in (
        <foreach collection="list" item="item" separator=",">
            (#{item.paperId}, #{item.questionId})
        </foreach>
        )
    </insert>

其他的代码参考批量新增,几乎一致不在占用版面。

总结

稍作修改性能提升几十倍,非常的爽

相关推荐
刘一说19 小时前
深入解析 Spring Boot 数据访问:Spring Data JPA 与 MyBatis 集成实战
spring boot·tomcat·mybatis
赋能大师兄20 小时前
MyBatis缓存机制
mybatis·二级缓存·一级缓存
codingPower2 天前
升级mybatis-plus导致项目启动报错: net.sf.jsqlparser.statement.select.SelectBody
java·spring boot·maven·mybatis
123461613 天前
互联网大厂Java面试:从Spring Boot到微服务的探索
java·数据库·spring boot·微服务·面试·mybatis·orm
2301_801252223 天前
Mybatis的添加和修改功能
java·开发语言·mybatis
W.Buffer4 天前
MyBatis 源码深度解析:从 Spring Boot 实战到底层原理
spring boot·后端·mybatis
CS Beginner4 天前
【搭建】个人博客网站的搭建
java·前端·学习·servlet·log4j·mybatis
m0_564264185 天前
IDEA DEBUG调试时如何获取 MyBatis-Plus 动态拼接的 SQL?
java·数据库·spring boot·sql·mybatis·debug·mybatis-plus
没有bug.的程序员6 天前
Spring Boot 整合第三方组件:Redis、MyBatis、Kafka 实战
java·spring boot·redis·后端·spring·bean·mybatis
迷了璐的航6 天前
mybatis解决查询中使用group by时出现sql_mode=only_full_group_by
数据库·sql·mybatis