亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

1 環(huán)境準(zhǔn)備

1.1 搭建 MyBatis-Plus 環(huán)境

  1. 創(chuàng)建 maven springboot 工程
  2. 導(dǎo)入依賴:web 啟動(dòng)器、jdbc、、JAVA 連接 MySQL、Lombok、druid 連接池啟動(dòng)器、mybatis-plus 啟動(dòng)器
  3. 編寫 yaml 配置文件,配置 druid 數(shù)據(jù)源、mybatis-plus

注意要點(diǎn):

  1. mApper 接口繼承 BaseMapper<實(shí)體類>
  2. service 接口繼承 IService<實(shí)體類>
  3. service 接口實(shí)現(xiàn)類繼承 ServiceImpl<mapper 接口, 實(shí)體類> 實(shí)現(xiàn) service 接口

1.2 批量插入測(cè)試接口

  1. MyBatis 批量插入接口
@GetMapping("/mybatis-batch-insert") public String mybatisBatchInsert(){ // 開始時(shí)間 long stime = System.currentTimeMillis(); // 批量插入 orderService.mySaveBatch(list); // 結(jié)束時(shí)間 long etime = System.currentTimeMillis(); return "mybatis 批量插入 1w 條數(shù)據(jù)的時(shí)間: " + (etime - stime) / 1000.0 + "秒"; }
對(duì)比 MyBatis 和 MyBatis-Plus 批量插入、批量更新的性能和區(qū)別

 


 
  1. Mybatis 的批量插入是調(diào)用 mapper 的批量插入接口,使用 標(biāo)簽拼接 sql 進(jìn)行插入
  2. Mybatis-Plus 批量插入接口
  3. @GetMapping("/mybatis-batch-insert") public String mybatisBatchInsert(){ // 開始時(shí)間 long stime = System.currentTimeMillis(); // 批量插入 orderService.mySaveBatch(list); // 結(jié)束時(shí)間 long etime = System.currentTimeMillis(); return "mybatis 批量插入 1w 條數(shù)據(jù)的時(shí)間: " + (etime - stime) / 1000.0 + "秒"; }
  4. MyBatis-Plus 的批量插入是調(diào)用 mybatis-plus 的 IService 接口的 saveBatch 進(jìn)行批量插入

1.3 批量更新測(cè)試接口

  1. MyBatis 批量更新接口
  2. @GetMapping("/mybatis-batch-update") public String mybatisBatchUpdate(){ long stime = System.currentTimeMillis(); orderService.myUpdateBatchById(updateList); long etime = System.currentTimeMillis(); return "mybatis 批量更新 1w 條數(shù)據(jù)的時(shí)間: " + (etime - stime) / 1000.0 + "秒"; }
  3. MyBatis 的批量插入是調(diào)用 mapper 的批量更新接口,使用 標(biāo)簽拼接 sql 進(jìn)行更新,是將多個(gè)更新語句拼接在同一個(gè) mapper 接口中,需要在數(shù)據(jù)庫連接 url 添加 allowMultiQueries=true 開啟多查詢
  4. MyBatis-Plus 批量更新接口
  5. @GetMapping("/mybatis-plus-batch-update") public String mybatisPlusBatchUpdate(){ long stime = System.currentTimeMillis(); orderService.updateBatchById(updateList); long etime = System.currentTimeMillis(); return "mybatis plus 批量更新 1w 條數(shù)據(jù)的時(shí)間: " + (etime - stime) / 1000.0 + "秒"; }
  6. MyBatis -Plus 的批量更新是調(diào)用 mybatis-plus 的 IService 接口的 updateBatchById 進(jìn)行批量更新

2. 性能對(duì)比

經(jīng)過預(yù)熱,盡量避免了緩存的影響。

2.1 批量插入性能對(duì)比

對(duì)比 MyBatis 和 MyBatis-Plus 批量插入、批量更新的性能和區(qū)別

 

數(shù)據(jù)量:1w 條數(shù)據(jù),每條數(shù)據(jù) 4 個(gè)字段

測(cè)試結(jié)果:

  • MyBatis:5 次運(yùn)行平均耗時(shí):0.3212 秒
  • MyBatis-Plus:5次運(yùn)行平均耗時(shí):1.35 秒
  • MyBatis-Plus(開啟批處理):5次運(yùn)行平均耗時(shí):0.2424 秒

2.2 批量更新性能對(duì)比

對(duì)比 MyBatis 和 MyBatis-Plus 批量插入、批量更新的性能和區(qū)別

 

數(shù)據(jù)量:1w 條數(shù)據(jù),每條數(shù)據(jù)更新 4 個(gè)字段

測(cè)試結(jié)果:

  • MyBatis:5 次運(yùn)行平均耗時(shí):1.0378 秒
  • MyBatis-Plus:5次運(yùn)行平均耗時(shí):1.6448 秒
  • MyBatis-Plus(開啟批處理):5次運(yùn)行平均耗時(shí):0.743 秒

3. 原理探究

3.1 批量插入

3.1.1 MyBatis

MyBatis 的批量插入 xml

<insert id="mySaveBatch">
    insert into order_table(id, product_id, total_amount, status) values
    <foreach collection="list" separator="," item="item">
        (#{item.id}, #{item.productId}, #{item.totalAmount}, #{item.status})
    </foreach>
</insert>

通過 標(biāo)簽,將插入的數(shù)據(jù)拼接成一條 SQL 語句,一次性進(jìn)行插入操作,拼接完的 SQL 語句如下例子:

insert into order_table(id, product_id, total_amount, status) values(1, 2. 2.0, 1),(2, 2, 2.0, 1);

3.1.2 MyBatis-Plus

MyBatis-Plus 的批量插入本質(zhì)采用 for 遍歷每條數(shù)據(jù)依次插入,但使用了批處理優(yōu)化,默認(rèn)是每 1000 條數(shù)據(jù),刷新一次 statement 提交到數(shù)據(jù)庫,執(zhí)行插入操作。

注意:批處理需要在數(shù)據(jù)庫連接中添加 rewriteBatchedStatements=true 否則 jdbc 驅(qū)動(dòng)在默認(rèn)情況下會(huì)無視executeBatch() 語句

源碼如下:

@Transactional(rollbackFor = Exception.class)   // 開啟事務(wù)
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
    String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);    // 獲得插入 statement
    // 執(zhí)行批處理操作
    // 參數(shù)是:待插入集合,批次大?。J(rèn)1000),函數(shù)式接口 accept
    return executeBatch(entityList, batchSize, (sqlSession, entity) ->  sqlSession.insert(sqlStatement, entity)); 
}
public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
    Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
    return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> {
        int size = list.size();
        int idxLimit = Math.min(batchSize, size);
        int i = 1;
        // 遍歷插入
        for (E element : list) {
            // 調(diào)用 sqlSession.insert(sqlStatement, entity)); 
            // 對(duì)元素執(zhí)行插入操作,但此時(shí)數(shù)據(jù)庫還沒真正執(zhí)行插入語句
            consumer.accept(sqlSession, element);   
            // 計(jì)數(shù)達(dá)到 1000 或者 集合結(jié)束
            if (i == idxLimit) {        
                 // 刷新 statement 提交批處理語句,數(shù)據(jù)庫真正執(zhí)行 SQL
                sqlSession.flushStatements();
                idxLimit = Math.min(idxLimit + batchSize, size);
            }
            i++;
        }
    });
}

3.2 批量更新

3.2.1 MyBatis

MyBatis 的批量更新 xml

<update id="myUpdateBatchById">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update order_table
        <set>
            product_id = #{item.productId},
            total_amount = #{item.totalAmount},
            status = #{item.status}
        </set>
        where id = #{item.id}
    </foreach>
</update>

通過 標(biāo)簽,拼接成多條更新的 SQL 語句,一次性提交數(shù)據(jù)庫執(zhí)行。語句例子如下:

update order_table set product_id = 1, total_amount = 2, status = 1 where id = 1;
update order_table set product_id = 1, total_amount = 2, status = 1 where id = 2;

3.1.2 MyBatis-Plus

MyBatis-Plus 批量更新的原理基本和其批量插入的原理一致,都是調(diào)用 executeBatch 執(zhí)行批處理操作。

4. 優(yōu)缺點(diǎn)分析

4.1 批量插入

對(duì)于批量插入,MyBatis 拼接 SQL 的寫法比 MyBatis-Plus 的批量插入方法有明顯更高的性能。
但在開啟批處理優(yōu)化之后,MyBatis-Plus 的方法性能更高了。

MyBatis:

  • 優(yōu)點(diǎn):性能較高
  • 缺點(diǎn):在處理大數(shù)據(jù)量(SQL 語句大于 64MB 時(shí))會(huì)報(bào)錯(cuò),MySQL 8.0.33 默認(rèn)最大 SQL 大小為 64MB
    也要考慮內(nèi)存異常等問題。

MyBatisPlus:

  • 優(yōu)點(diǎn):分組提交,適用于大數(shù)據(jù)量的處理
  • 缺點(diǎn):單條插入語句執(zhí)行,性能較低

總結(jié):

  • 沒開啟批處理時(shí):10w 數(shù)據(jù)量以下建議使用 MyBatis、10w 數(shù)據(jù)量以上建議使用 MyBatis-Plus
  • 開啟批處理時(shí):建議使用 MyBatis-Plus

4.2 批量更新

對(duì)于批量更新,MyBatis 拼接 SQL 的寫法與 MyBatis-Plus 的批量更新方法無明顯的性能差別.
大于大數(shù)據(jù)量,拼接寫法甚至容易出現(xiàn)內(nèi)存耗盡等問題,相比之下 MyBatis-Plus 的方法更優(yōu)。

總結(jié):建議使用 MyBatis-Plus

分享到:
標(biāo)簽:MyBatis
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績?cè)u(píng)定