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

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

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

mybatis查詢默認(rèn)是一次獲取全部, 有時(shí)候需要查詢上萬上百萬數(shù)據(jù)時(shí),如果一次性讀取到內(nèi)存中,會(huì)容易導(dǎo)致OOM問題。這時(shí)候需要采用流式查詢。以下擴(kuò)展了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組合成一個(gè)接口

/**
 * 流式查詢接口
 * @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)于擴(kuò)展tk.mybatis的流式查詢功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)tk.mybatis 流式查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!?

相關(guān)文章

  • 一文帶你了解SpringBoot中啟動(dòng)參數(shù)的各種用法

    一文帶你了解SpringBoot中啟動(dòng)參數(shù)的各種用法

    在使用 Spring Boot開發(fā)應(yīng)用時(shí),我們通常需要根據(jù)不同的環(huán)境或特定需求調(diào)整啟動(dòng)參數(shù),那么,Spring Boot 提供了哪些方式來配置這些啟動(dòng)參數(shù)呢,下面小編就來和大家介紹一下吧
    2025-03-03
  • Java FTPClient實(shí)現(xiàn)文件上傳下載

    Java FTPClient實(shí)現(xiàn)文件上傳下載

    這篇文章主要為大家詳細(xì)介紹了Java FTPClient實(shí)現(xiàn)文件上傳下載的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Spring Cloud Feign簡單使用詳解

    Spring Cloud Feign簡單使用詳解

    本篇文章主要介紹了Spring Cloud Feign簡單使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • 關(guān)于Intellij idea 報(bào)錯(cuò):Error : java 不支持發(fā)行版本5的問題

    關(guān)于Intellij idea 報(bào)錯(cuò):Error : java 不支持發(fā)行版本5的問題

    這篇文章主要介紹了關(guān)于Intellij idea 報(bào)錯(cuò):Error : java 不支持發(fā)行版本5的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • java實(shí)現(xiàn)歸并排序算法

    java實(shí)現(xiàn)歸并排序算法

    在學(xué)習(xí)算法的過程中,我們難免會(huì)接觸很多和排序相關(guān)的算法。總而言之,對(duì)于任何編程人員來說,基本的排序算法是必須要掌握的。那么現(xiàn)在我們將要進(jìn)行基本的歸并排序算法的講解
    2016-01-01
  • SpringBoot中的maven插件spring-boot-maven-plugin使用

    SpringBoot中的maven插件spring-boot-maven-plugin使用

    這篇文章主要介紹了SpringBoot中的maven插件spring-boot-maven-plugin使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)

    Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)

    大家在使用Intellij IDEA 的時(shí)候會(huì)經(jīng)常遇到各種亂碼問題,今天小編給大家分享一些關(guān)于Intellij IDEA 2019 最新亂碼問題及解決必殺技,感興趣的朋友跟隨小編一起看看吧
    2020-04-04
  • eclipse中maven的pom.xml文件中增加依賴的方法

    eclipse中maven的pom.xml文件中增加依賴的方法

    日 在Maven項(xiàng)目中,可以使用pom.xml文件來添加依賴包,本文主要介紹了eclipse中maven的pom.xml文件中增加依賴的方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • Spring Boot 2.X快速整合jpa過程解析

    Spring Boot 2.X快速整合jpa過程解析

    這篇文章主要介紹了Spring Boot 2.X 如何快速整合jpa?,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • jenkins自動(dòng)構(gòu)建SpringCloud流程

    jenkins自動(dòng)構(gòu)建SpringCloud流程

    文章介紹了如何使用Jenkins和docker-compose自動(dòng)部署SpringCloud和Vue項(xiàng)目,首先,下載并安裝Jenkins,并配置Jenkins以自動(dòng)啟動(dòng),然后,配置GitLab插件和NodeJS插件,以便可以從GitLab倉庫中獲取代碼并構(gòu)建,接下來,創(chuàng)建一個(gè)Jenkins任務(wù)來構(gòu)建Vue項(xiàng)目
    2025-02-02

最新評(píng)論

寿阳县| 尼玛县| 西充县| 天门市| 松阳县| 北碚区| 朝阳市| 栖霞市| 佛坪县| 牡丹江市| 开远市| 定兴县| 丹东市| 济宁市| 马尔康县| 佳木斯市| 肇东市| 文成县| 驻马店市| 通山县| 贵溪市| 涟水县| 寻乌县| 庄河市| 历史| 莎车县| 温宿县| 宜君县| 延长县| 彭阳县| 乌鲁木齐市| 舒城县| 朝阳市| 九寨沟县| 菏泽市| 怀安县| 浪卡子县| 宁蒗| 岚皋县| 兰溪市| 鲁甸县|