欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

擴展tk.mybatis的流式查詢功能實現(xiàn)

 更新時間:2021年12月01日 15:41:47   作者:sunct  
mybatis查詢默認(rèn)是一次獲取全部,如果數(shù)據(jù)過于龐大,就會導(dǎo)致OOM問題,本文就介紹了tk.mybatis 流式查詢,具有一定的參考價值,感興趣的可以了解一下

mybatis查詢默認(rèn)是一次獲取全部, 有時候需要查詢上萬上百萬數(shù)據(jù)時,如果一次性讀取到內(nèi)存中,會容易導(dǎo)致OOM問題。這時候需要采用流式查詢。以下擴展了tk.mybatis的流式查詢功能。 直接上干貨:

@Options注解是關(guān)鍵

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;

/**
 * 通用Mapper接口,流式查詢
 *
 * @param <T> 不能為空
 * @author sunchangtan
 */
@tk.mybatis.mapper.annotation.RegisterMapper
public interface SelectStreamByExampleMapper<T> {

    /**
     * 根據(jù)example條件和RowBounds進(jìn)行流式查詢
     *
     * @param example
     * @return
     */
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    @SelectProvider(type = StreamExampleProvider.class, method = "dynamicSQL")
    void selectStreamByExampleMapper(Object example, ResultHandler resultHandler);

}

帶RowBounds的流式查詢

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
 * 通用Mapper接口,流式查詢
 *
 * @param <T> 不能為空
 * @author sunchangtan
 */
@tk.mybatis.mapper.annotation.RegisterMapper
public interface SelectStreamByExampleRowBoundsMapper<T> {

    /**
     * 根據(jù)example條件和RowBounds進(jìn)行流式查詢
     *
     * @param example
     * @return
     */
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    @SelectProvider(type = StreamExampleProvider.class, method = "dynamicSQL")
    void selectStreamByExampleRowBoundsMapper(Object example, RowBounds rowBounds, ResultHandler resultHandler);

}

流式ExampleProvider

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.provider.ExampleProvider;

/**
 * 流式查詢的SqlProvider
 * @author sunchangtan
 */

public class StreamExampleProvider extends ExampleProvider {

    public StreamExampleProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }


    /**
     * 根據(jù)Example流式查詢
     *
     * @param ms
     * @return
     */
    public String selectStreamByExampleMapper(MappedStatement ms) {
        return this.selectByExample(ms);
    }

    /**
     * 根據(jù)Example和RowBounds流式查詢
     * @param ms
     * @return
     */
    public String selectStreamByExampleRowBoundsMapper(MappedStatement ms) {
        return this.selectByExample(ms);
    }

}

將SelectStreamByExampleMapper和SelectStreamByExampleRowBoundsMapper組合成一個接口

/**
 * 流式查詢接口
 * @param <T>
 * @author sunchangtan
 */
@tk.mybatis.mapper.annotation.RegisterMapper
public interface StreamMapper<T> extends
        SelectStreamByExampleMapper<T>,
        SelectStreamByExampleRowBoundsMapper<T> {
}

在BaseMapper中加入StreamMapper

/**
 * Mapper的基類
 * @author sunchangtan
 * @param <T>
 */
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, StreamMapper<T> {
}

使用例子:

this.userMapper.selectStreamByExampleRowBoundsMapper(example, new RowBounds(0, 1), resultContext -> {
     User user= (User) resultContext.getResultObject();
     System.out.println(user);
 });


this.userMapper.selectStreamByExampleMapper(example, resultContext -> {
    User user= (User) resultContext.getResultObject();
    System.out.println(User);
});

到此這篇關(guān)于擴展tk.mybatis的流式查詢功能實現(xiàn)的文章就介紹到這了,更多相關(guān)tk.mybatis 流式查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!?

相關(guān)文章

  • Spring中FactoryBean的高級用法實戰(zhàn)教程

    Spring中FactoryBean的高級用法實戰(zhàn)教程

    FactoryBean是Spring框架的高級特性,允許自定義對象的創(chuàng)建過程,適用于復(fù)雜初始化邏輯,本文給大家介紹Spring中FactoryBean的高級用法實戰(zhàn),感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Spring循環(huán)依賴的處理方法

    Spring循環(huán)依賴的處理方法

    循環(huán)依賴是指兩個或多個組件之間相互依賴,形成一個閉環(huán),從而導(dǎo)致這些組件無法正確地被初始化或加載,這篇文章主要介紹了Spring循環(huán)依賴的處理,需要的朋友可以參考下
    2023-08-08
  • Spring Cloud Gateway層限流實現(xiàn)過程

    Spring Cloud Gateway層限流實現(xiàn)過程

    這篇文章主要介紹了Spring Cloud Gateway層限流實現(xiàn)過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java語言一元運算符實例解析

    Java語言一元運算符實例解析

    這篇文章主要介紹了Java語言中的一元運算符實例解析,需要的朋友可以參考下。
    2017-09-09
  • struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成簡單demo

    struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成簡單demo

    本篇文章主要介紹了struts2.2.3+spring3.1.0 + mybatis3.1.0框架整合,結(jié)合在一起實現(xiàn)用戶的增刪改查功能,有需要的可以了解一下。
    2016-11-11
  • 你的Idea還有BUG嗎不妨試試另一個開發(fā)神器

    你的Idea還有BUG嗎不妨試試另一個開發(fā)神器

    Spring Tool Suite(STS)就是一個基于Eclipse的開發(fā)環(huán)境, 用于開發(fā)Spring應(yīng)用程序。本文給大家給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-12-12
  • Java實現(xiàn)簡單日歷小程序 Java圖形界面小日歷開發(fā)

    Java實現(xiàn)簡單日歷小程序 Java圖形界面小日歷開發(fā)

    這篇文章主要介紹了Java實現(xiàn)簡單日歷小程序,如何用Java swing開發(fā)一款簡單的小日歷,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 使用jpa原生sql@Query操作增刪改查

    使用jpa原生sql@Query操作增刪改查

    這篇文章主要介紹了使用jpa原生sql@Query操作增刪改查,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java8的Optional如何干掉空指針(示例詳解)

    Java8的Optional如何干掉空指針(示例詳解)

    這篇文章主要介紹了Java8的Optional如何干掉空指針,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • maven繼承父工程統(tǒng)一版本號的實現(xiàn)

    maven繼承父工程統(tǒng)一版本號的實現(xiàn)

    這篇文章主要介紹了maven繼承父工程統(tǒng)一版本號的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評論