擴展tk.mybatis的流式查詢功能實現(xiàn)
mybatis查詢默認是一次獲取全部, 有時候需要查詢上萬上百萬數(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進行流式查詢
*
* @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進行流式查詢
*
* @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)文章
一文帶你了解SpringBoot中啟動參數(shù)的各種用法
在使用 Spring Boot開發(fā)應(yīng)用時,我們通常需要根據(jù)不同的環(huán)境或特定需求調(diào)整啟動參數(shù),那么,Spring Boot 提供了哪些方式來配置這些啟動參數(shù)呢,下面小編就來和大家介紹一下吧2025-03-03
關(guān)于Intellij idea 報錯:Error : java 不支持發(fā)行版本5的問題
這篇文章主要介紹了關(guān)于Intellij idea 報錯:Error : java 不支持發(fā)行版本5的問題,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
SpringBoot中的maven插件spring-boot-maven-plugin使用
這篇文章主要介紹了SpringBoot中的maven插件spring-boot-maven-plugin使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)
大家在使用Intellij IDEA 的時候會經(jīng)常遇到各種亂碼問題,今天小編給大家分享一些關(guān)于Intellij IDEA 2019 最新亂碼問題及解決必殺技,感興趣的朋友跟隨小編一起看看吧2020-04-04
eclipse中maven的pom.xml文件中增加依賴的方法
日 在Maven項目中,可以使用pom.xml文件來添加依賴包,本文主要介紹了eclipse中maven的pom.xml文件中增加依賴的方法,具有一定的參考價值,感興趣的可以了解一下2023-12-12

