您好,登錄后才能下訂單哦!
本篇內容主要講解“擴展tk.mybatis的流式查詢功能如何實現”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“擴展tk.mybatis的流式查詢功能如何實現”吧!
mybatis查詢默認是一次獲取全部, 有時候需要查詢上萬上百萬數據時,如果一次性讀取到內存中,會容易導致OOM問題。這時候需要采用流式查詢。以下擴展了tk.mybatis的流式查詢功能。
@Options注解是關鍵
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> { /** * 根據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> { /** * 根據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); } /** * 根據Example流式查詢 * * @param ms * @return */ public String selectStreamByExampleMapper(MappedStatement ms) { return this.selectByExample(ms); } /** * 根據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); });
到此,相信大家對“擴展tk.mybatis的流式查詢功能如何實現”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。