mybatis mysql数据库批量操作

mybatis mysql数据库批量操作

在我们写代码的时候经常会进行一些批量操作,不经常写的话会忘记。在此仅记录一下常用的sql代码。

1. 批量新增

Mapper文件

int batchInsert(List<NoticeDetail> list);

Mapper.xml文件

<insert id="batchInsert">
INSERT INTO notice_scope_detail (notice_id, obj_id)
      VALUES
      <foreach collection="list" item="item" index="index" separator=",">
        (#{item.noticeId}, #{item.objId})
      </foreach>
</insert>

2. 批量新增并更新

数据量太大的时候不能一下子全部更新,要分批次进行操作。

java代码

		List<MigrateEtcHandoverLogKey> list =  	mysqlMigrateTableDaoService.selectHandoverList(dto.getBranchNo());
        if (list == null || list.isEmpty()) {
            return BeeResponseEntity.success();
        }
        // 分批次插入,每次100条
        int batchSize = Math.min(list.size(), 100);
        for (int i = 0; i < list.size(); i += batchSize) {
            int endIndex = Math.min(list.size(), i + batchSize);
            List<MigrateEtcHandoverLogKey> batchList = list.subList(i, endIndex);
            // 
            oracleMigrateTableDaoService.batchInsertOrUpdate(batchList);
        }
        return BeeResponseEntity.success();

Mapper文件

int batchInsertOrUpdate(List<NoticeDetail> list);

Mapper.xml文件

方法一:DUPLICATE KEY

<insert id="batchInsertOrUpdate">
    INSERT INTO notice_scope_detail (notice_id, obj_id)
    VALUES
    <foreach collection="list" item="item" separator=",">
      (#{item.noticeId}, #{item.objId})
    </foreach>
    ON DUPLICATE KEY UPDATE
    notice_id = VALUES(notice_id),
    obj_id = VALUES(obj_id);
  </insert>

方法二: replace into

<insert id="batchInsertOrUpdate">
    replace into notice_scope_detail (notice_id, obj_id)
    VALUES
    <foreach collection="list" item="item" separator=",">
      (#{item.noticeId}, #{item.objId})
    </foreach>
  </insert>

3.批量更新

Mapper文件

int updateBatch(List<NoticeDetail> list);

Mapper.xml文件

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update tableName
        <set>
            name=${item.name},
            name2=${item.name2}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行。所以需要在连接mysql的url上加 &allowMultiQueries=true 这个才可以执行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis提供了一种简单的方式来完成MySQL批量操作,可以通过使用`<foreach>`标签来实现。 下面是一个示例,展示如何使用MyBatis进行MySQL批量插入操作: 首先,创建一个Mapper接口,定义批量插入的方法: ```java public interface UserMapper { void insertBatch(List<User> userList); } ``` 然后,在对应的Mapper XML文件中,使用`<foreach>`标签来实现批量插入: ```xml <insert id="insertBatch" parameterType="java.util.List"> INSERT INTO user (id, name, age) VALUES <foreach collection="list" item="user" separator=","> (#{user.id}, #{user.name}, #{user.age}) </foreach> </insert> ``` 在上述示例中,`<insert>`标签定义了一个批量插入的SQL语句。通过`<foreach>`标签,我们可以遍历传入的`List<User>`,并将每个User对象的属性值作为参数传递到SQL语句中。 最后,通过MyBatisSqlSession执行批量插入操作: ```java List<User> userList = new ArrayList<>(); // 添加要插入的用户对象到userList中 try (SqlSession sqlSession = sqlSessionFactory.openSession()) { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.insertBatch(userList); sqlSession.commit(); } ``` 在上述代码中,我们获取了UserMapper接口的实例,并调用insertBatch方法来执行批量插入操作。最后,我们需要记得调用`sqlSession.commit()`来提交事务。 通过以上步骤,就可以使用MyBatis完成MySQL批量操作了。需要注意的是,批量操作的性能会受到数据库和网络的影响,因此在实际应用中,可以根据具体情况进行性能优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值