springboot怎么整合mybatis实现数据库的更新批处理

其他教程   发布日期:2023年08月21日   浏览次数:463

本文小编为大家详细介绍“springboot怎么整合mybatis实现数据库的更新批处理”,内容详细,步骤清晰,细节处理妥当,希望这篇“springboot怎么整合mybatis实现数据库的更新批处理”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    springboot整合mybatis实现数据库更新批处理

    1.在mapper接口中编写方法

    1. /**
    2. * 修改book表中的销量和库存
    3. * 要使用批处理
    4. */
    5. Integer batchBookCountStork(@Param("bookList") List<CartItem> bookList);

    2.在mapper.xml中编写对相应的更新sql语句

    1. <update id="batchBookCountStork" parameterType="java.util.List">
    2. UPDATE t_book
    3. <set>
    4. <foreach collection="bookList" item="book" index="index" open="`sales` = CASE `book_id`" close="END,">
    5. WHEN #{book.bookId} THEN sales+#{book.count}
    6. </foreach>
    7. <foreach collection="bookList" item="book" index="index" open="`stock` = CASE `book_id`" close="END,">
    8. WHEN #{book.bookId} THEN stock-#{book.count}
    9. </foreach>
    10. </set>
    11. <where>
    12. <foreach collection="bookList" item="book" index="index" open="`book_id` IN(" close=")" separator=",">
    13. #{book.bookId}
    14. </foreach>
    15. </where>
    16. </update>

    3.这个配置文件的sql语句流程如下:

    1. update t_book(表名)
    2. set sales(这个是数据库的销量字段名) = case book_id(这个是数据库的id字段名)
    3. when bookid(从list集合中取出来的) then sales+(从集合中取出的数据)
    4. ...(这里可以一直进行拼接)
    5. end,
    6. stock(这个是数据库的库存字段名) = CASE book_id(这个是数据库的id字段名)
    7. when bookid(从list集合中取出来的) then stock-(从集合中取出数据)
    8. ...(这里可以一直进行拼接)
    9. end,
    10. where `book_id`(这个是数据库的id字段名) IN(bookid(从list集合中取出来),bookid(从list集合中取出来)...)

    4.这个sql语句的含义:

    更新表里面的数据根据集合遍历出来的id值,设置要更新的字段名,让要更新的字段值跟这个表的主键id进行绑定,当这个主键id与list中取出来的id值一致时就让这个要更新的字段名,取then后面的值

    Mybatis批量更新数据库 MybatisBatchUtils batchInsertupdate spring boot

    MybatisBatchUtils

    1. int cnt = mybatisBatchUtils.batchUpdateOrInsert(addList, UiConfigDetailMapper.class,
    2. (item, uiConfigDetailMapper) -> uiConfigDetailMapper.insertSelective(item));
    1. package cn.XXX.dao.serivce.common;
    2. import com.XXX.doctorusercenter.exception.BusinessException;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.apache.ibatis.session.ExecutorType;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import org.springframework.stereotype.Component;
    8. import javax.annotation.Resource;
    9. import java.util.List;
    10. import java.util.function.BiFunction;
    11. @Slf4j
    12. @Component
    13. public class MybatisBatchUtils {
    14. /**
    15. * 每次处理1000条
    16. */
    17. private static final int BATCH_SIZE = 1000;
    18. @Resource
    19. private SqlSessionFactory sqlSessionFactory;
    20. /**
    21. * 批量处理修改或者插入
    22. *
    23. * @param data 需要被处理的数据
    24. * @param mapperClass Mybatis的Mapper类
    25. * @param function 自定义处理逻辑
    26. * @return int 影响的总行数
    27. */
    28. public <T, U, R> int batchUpdateOrInsert(List<T> data, Class<U> mapperClass, BiFunction<T, U, R> function) {
    29. int i = 1;
    30. SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
    31. try {
    32. U mapper = batchSqlSession.getMapper(mapperClass);
    33. int size = data.size();
    34. for (T element : data) {
    35. function.apply(element, mapper);
    36. if ((i % BATCH_SIZE == 0) || i == size) {
    37. batchSqlSession.flushStatements();
    38. }
    39. i++;
    40. }
    41. // 非事务环境下强制commit,事务情况下该commit相当于无效
    42. batchSqlSession.commit(true);
    43. } catch (Exception e) {
    44. batchSqlSession.rollback();
    45. // throw new BusinessException(e.getMessage());
    46. log.error("batchUpdateOrInsert", e);
    47. } finally {
    48. batchSqlSession.close();
    49. }
    50. return i - 1;
    51. }
    52. }

    以上就是springboot怎么整合mybatis实现数据库的更新批处理的详细内容,更多关于springboot怎么整合mybatis实现数据库的更新批处理的资料请关注九品源码其它相关文章!