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>

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

总结

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

相关推荐
DokiDoki之父7 小时前
MyBatis—增删查改操作
java·spring boot·mybatis
DokiDoki之父14 小时前
Mybatis—入门 & (配置)SQL提示和日志输出
数据库·sql·mybatis
计算机学姐2 天前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
哲此一生9843 天前
SpringBoot3集成Mybatis(开启第一个集成Mybatis的后端接口)
java·spring boot·mybatis
九转苍翎3 天前
Java外功精要(3)——Spring配置文件和mybatis
spring boot·mybatis
程序员三明治3 天前
【Mybatis从入门到入土】ResultMap映射、多表查询与缓存机制全解析
java·sql·缓存·mybatis·resultmap·缓存机制·多表查询
此剑之势丶愈斩愈烈3 天前
mybatis-plus分页插件使用
mybatis
!if3 天前
springboot mybatisplus 配置SQL日志,但是没有日志输出
spring boot·sql·mybatis
讓丄帝愛伱3 天前
Mybatis Log Free插件使用
java·开发语言·mybatis
gaoshan123456789103 天前
‌MyBatis-Plus 的 LambdaQueryWrapper 可以实现 OR 条件查询‌
java·tomcat·mybatis