springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫的更新批處理方式
springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫更新批處理
1.在mapper接口中編寫方法
/** ?* 修改book表中的銷量和庫存 ?* 要使用批處理 ?*/ Integer batchBookCountStork(@Param("bookList") List<CartItem> bookList);
2.在mapper.xml中編寫對(duì)相應(yīng)的更新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.這個(gè)配置文件的sql語句流程如下:
update t_book(表名) set sales(這個(gè)是數(shù)據(jù)庫的銷量字段名) = case book_id(這個(gè)是數(shù)據(jù)庫的id字段名) ? ? when bookid(從list集合中取出來的) then sales+(從集合中取出的數(shù)據(jù)) ? ? ...(這里可以一直進(jìn)行拼接) ? end, ? ? stock(這個(gè)是數(shù)據(jù)庫的庫存字段名) = CASE book_id(這個(gè)是數(shù)據(jù)庫的id字段名) ? ? when bookid(從list集合中取出來的) then stock-(從集合中取出數(shù)據(jù)) ? ? ...(這里可以一直進(jìn)行拼接) ? end, where `book_id`(這個(gè)是數(shù)據(jù)庫的id字段名) IN(bookid(從list集合中取出來),bookid(從list集合中取出來)...)
4.這個(gè)sql語句的含義:
更新表里面的數(shù)據(jù)根據(jù)集合遍歷出來的id值,設(shè)置要更新的字段名,讓要更新的字段值跟這個(gè)表的主鍵id進(jìn)行綁定,當(dāng)這個(gè)主鍵id與list中取出來的id值一致時(shí)就讓這個(gè)要更新的字段名,取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++; } // 非事務(wù)環(huán)境下強(qiáng)制commit,事務(wù)情況下該commit相當(dāng)于無效 batchSqlSession.commit(true); } catch (Exception e) { batchSqlSession.rollback(); // throw new BusinessException(e.getMessage()); log.error("batchUpdateOrInsert", e); } finally { batchSqlSession.close(); } return i - 1; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot外部化配置實(shí)戰(zhàn)解析
這篇文章主要介紹了Spring Boot外部化配置實(shí)戰(zhàn)解析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06Sharding-JDBC自動(dòng)實(shí)現(xiàn)MySQL讀寫分離的示例代碼
本文主要介紹了Sharding-JDBC自動(dòng)實(shí)現(xiàn)MySQL讀寫分離,優(yōu)點(diǎn)在于數(shù)據(jù)源完全有Sharding-JDBC托管,寫操作自動(dòng)執(zhí)行master庫,讀操作自動(dòng)執(zhí)行slave庫,感興趣的可以了解一下2021-11-11java實(shí)現(xiàn)文件上傳的詳細(xì)步驟
文件上傳是用戶將本地文件通過Web頁面提交到服務(wù)器的過程,涉及客戶端、服務(wù)器端、上傳表單等組件,在SpringBoot中,通過MultipartFile接口處理上傳文件,并將其保存在服務(wù)器,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題
這篇文章主要介紹了JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題,堆和棧得速度性能分析多角度給大家分析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08java 實(shí)現(xiàn)最小二叉樹堆排序的實(shí)例
這篇文章主要介紹了java 實(shí)現(xiàn)最小二叉樹堆排序的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09IDEA項(xiàng)目代碼上傳gitlab遠(yuǎn)程倉庫過程圖解
這篇文章主要介紹了IDEA項(xiàng)目代碼上傳gitlab遠(yuǎn)程倉庫過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09