Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the streaming query function of extended tk.mybatis

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to expand the streaming query function of tk.mybatis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to expand the streaming query function of tk.mybatis.

Mybatis query acquires all at once by default, and sometimes when tens of millions of data need to be queried, if it is read into memory at once, it will easily lead to OOM problems. At this point, you need to use streaming query. The following extends the streaming query capabilities of tk.mybatis.

@ Options annotations are the key

Import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.SelectProvider;import org.apache.ibatis.mapping.ResultSetType;import org.apache.ibatis.session.ResultHandler / * * Universal Mapper interface. Streaming query * * @ param cannot be empty * @ author sunchangtan * / @ tk.mybatis.mapper.annotation.RegisterMapperpublic interface SelectStreamByExampleMapper {/ * perform streaming query according to example conditions and RowBounds * * @ param example * @ return * / @ Options (resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000) @ SelectProvider (type = StreamExampleProvider.class) Method = "dynamicSQL") void selectStreamByExampleMapper (Object example, ResultHandler resultHandler) }

Streaming query with 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 / * * Universal Mapper interface. Streaming query * * @ param cannot be empty * @ author sunchangtan * / @ tk.mybatis.mapper.annotation.RegisterMapperpublic interface SelectStreamByExampleRowBoundsMapper {/ * perform streaming query according to example conditions and 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) }

Streaming ExampleProvider

SqlProvider * @ author sunchangtan * / public class StreamExampleProvider extends ExampleProvider {public StreamExampleProvider (Class mapperClass, MapperHelper mapperHelper) {super (mapperClass, mapperHelper) of import org.apache.ibatis.mapping.MappedStatement;import tk.mybatis.mapper.mapperhelper.MapperHelper;import tk.mybatis.mapper.provider.ExampleProvider;/** * streaming query } / * * streaming query according to Example * * @ param ms * @ return * / public String selectStreamByExampleMapper (MappedStatement ms) {return this.selectByExample (ms);} / * * streaming query based on Example and RowBounds * @ param ms * @ return * / public String selectStreamByExampleRowBoundsMapper (MappedStatement ms) {return this.selectByExample (ms);}}

Combine SelectStreamByExampleMapper and SelectStreamByExampleRowBoundsMapper into a single interface

/ * * streaming query API * @ param * @ author sunchangtan * / @ tk.mybatis.mapper.annotation.RegisterMapperpublic interface StreamMapper extends SelectStreamByExampleMapper, SelectStreamByExampleRowBoundsMapper {}

Add StreamMapper to BaseMapper

/ * * Mapper base class * @ author sunchangtan * @ param * / public interface BaseMapper extends Mapper, MySqlMapper, StreamMapper {}

Examples of use:

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);}); at this point, I believe you have a deeper understanding of how to extend tk.mybatis 's streaming query function, so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report