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>

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

总结

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

相关推荐
半夏陌离4 小时前
SQL 进阶指南:视图的创建与使用(视图语法 / 作用 / 权限控制)
java·数据库·mybatis
BillKu7 小时前
Spring Boot中MyBatis的定义与使用
spring boot·mybatis
lozhyf8 小时前
固定资产管理系统(vue+Springboot+mybatis)
vue.js·spring boot·mybatis
热烈勒温10 小时前
Mybatis入门、操作数据、配置xml映射、数据封装
xml·mybatis
javadaydayup1 天前
为什么 MyBatis Mapper 接口能像普通 Bean 一样被 @Autowired?
后端·mybatis
小趴菜不能喝2 天前
Spring boot3.x整合mybatis-plus踩坑记录
java·spring boot·mybatis
焯7592 天前
若依微服务遇到的配置问题
java·mybatis·ruoyi
颜如玉3 天前
MybatisPlus Sql Inject魔法🪄
后端·mybatis·源码
叫我阿柒啊3 天前
从全栈开发到微服务架构:一位Java工程师的实战经验分享
java·ci/cd·kafka·mybatis·vue3·springboot·fullstack
LB21123 天前
MyBatis xml配置文件
xml·java·mybatis