最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot整合mybatis實現(xiàn)數(shù)據(jù)庫的更新批處理方式

 更新時間:2023年03月12日 16:21:27   作者:qq_1757537040  
這篇文章主要介紹了springboot整合mybatis實現(xiàn)數(shù)據(jù)庫的更新批處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot整合mybatis實現(xiàn)數(shù)據(jù)庫更新批處理

1.在mapper接口中編寫方法

/**
?* 修改book表中的銷量和庫存
?* 要使用批處理
?*/
Integer batchBookCountStork(@Param("bookList") List<CartItem> bookList);

2.在mapper.xml中編寫對相應的更新sql語句

<update id="batchBookCountStork" parameterType="java.util.List">
? ? UPDATE t_book
? ? <set>
? ? ? ? <foreach collection="bookList" item="book" index="index" open="`sales` = CASE `book_id`" close="END,">
? ? ? ? ? ? WHEN #{book.bookId} THEN sales+#{book.count}
? ? ? ? </foreach>
? ? ? ? <foreach collection="bookList" item="book" index="index" open="`stock` = CASE `book_id`" close="END,">
? ? ? ? ? ? WHEN #{book.bookId} THEN stock-#{book.count}
? ? ? ? </foreach>
? ? </set>
? ? <where>
? ? ? ? <foreach collection="bookList" item="book" index="index" open="`book_id` IN(" close=")" separator=",">
? ? ? ? ? ? #{book.bookId}
? ? ? ? </foreach>
? ? </where>
? </update>

3.這個配置文件的sql語句流程如下:

update t_book(表名)
set sales(這個是數(shù)據(jù)庫的銷量字段名) = case book_id(這個是數(shù)據(jù)庫的id字段名)
? ? when bookid(從list集合中取出來的) then sales+(從集合中取出的數(shù)據(jù))
? ? ...(這里可以一直進行拼接)
? end,
? ? stock(這個是數(shù)據(jù)庫的庫存字段名) = CASE book_id(這個是數(shù)據(jù)庫的id字段名)
? ? when bookid(從list集合中取出來的) then stock-(從集合中取出數(shù)據(jù))
? ? ...(這里可以一直進行拼接)
? end,
where `book_id`(這個是數(shù)據(jù)庫的id字段名) IN(bookid(從list集合中取出來),bookid(從list集合中取出來)...)

4.這個sql語句的含義:

更新表里面的數(shù)據(jù)根據(jù)集合遍歷出來的id值,設置要更新的字段名,讓要更新的字段值跟這個表的主鍵id進行綁定,當這個主鍵id與list中取出來的id值一致時就讓這個要更新的字段名,取then后面的值

Mybatis批量更新數(shù)據(jù)庫 MybatisBatchUtils batchInsertupdate spring boot

MybatisBatchUtils

    int cnt = mybatisBatchUtils.batchUpdateOrInsert(addList, UiConfigDetailMapper.class,
                            (item, uiConfigDetailMapper) -> uiConfigDetailMapper.insertSelective(item));
package cn.XXX.dao.serivce.common;

import com.XXX.doctorusercenter.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.BiFunction;


@Slf4j
@Component
public class MybatisBatchUtils {

    /**
     * 每次處理1000條
     */
    private static final int BATCH_SIZE = 1000;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 批量處理修改或者插入
     *
     * @param data        需要被處理的數(shù)據(jù)
     * @param mapperClass Mybatis的Mapper類
     * @param function    自定義處理邏輯
     * @return int 影響的總行數(shù)
     */
    public <T, U, R> int batchUpdateOrInsert(List<T> data, Class<U> mapperClass, BiFunction<T, U, R> function)  {
        int i = 1;
        SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        try {
            U mapper = batchSqlSession.getMapper(mapperClass);
            int size = data.size();
            for (T element : data) {
              function.apply(element, mapper);
                if ((i % BATCH_SIZE == 0) || i == size) {
                    batchSqlSession.flushStatements();
                }
                i++;
            }
            // 非事務環(huán)境下強制commit,事務情況下該commit相當于無效
            batchSqlSession.commit(true);
        } catch (Exception e) {
            batchSqlSession.rollback();
            // throw new BusinessException(e.getMessage());
            log.error("batchUpdateOrInsert", e);
        } finally {
            batchSqlSession.close();
        }
        return i - 1;
    }
}

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

广安市| 汶上县| 兰州市| 民权县| 澄江县| 英超| 扎鲁特旗| 湘潭市| 阳新县| 百色市| 泸溪县| 宁阳县| 阜平县| 清苑县| 绵竹市| 内乡县| 重庆市| 得荣县| 上高县| 蛟河市| 南京市| 铁岭县| 贞丰县| 丰城市| 台湾省| 格尔木市| 弋阳县| 临西县| 乐业县| 隆尧县| 乐昌市| 襄垣县| 浠水县| 凤山市| 逊克县| 伽师县| 兰西县| 义马市| 保定市| 古田县| 全州县|