Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 08:56:47 10月13日 Update

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!

Tags: Query function query function interface content condition learning practical deeper one-off example key interest memory practicality practical simple operation data method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi OPPO Reno MariaDB Microsoft