本篇内容主要讲解“扩展tk.mybatis的流式查询功能如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“扩展tk.mybatis的流式查询功能如何实现”吧!mybatis查询默认是一次获取
本篇内容主要讲解“扩展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;@tk.mybatis.mapper.annotation.ReGISterMapperpublic interface SelectStreamByExampleMapper<T> { @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;@tk.mybatis.mapper.annotation.RegisterMapperpublic interface SelectStreamByExampleRowBoundsMapper<T> { @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;public class StreamExampleProvider extends ExampleProvider { public StreamExampleProvider(Class<?> mapperClass, MapperHelper mapperHelper) { super(mapperClass, mapperHelper); } public String selectStreamByExampleMapper(MappedStatement ms) { return this.selectByExample(ms); } public String selectStreamByExampleRowBoundsMapper(MappedStatement ms) { return this.selectByExample(ms); }}
将SelectStreamByExampleMapper和SelectStreamByExampleRowBoundsMapper组合成一个接口
@tk.mybatis.mapper.annotation.RegisterMapperpublic interface StreamMapper<T> extends SelectStreamByExampleMapper<T>, SelectStreamByExampleRowBoundsMapper<T> {}
在BaseMapper中加入StreamMapper
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的流式查询功能如何实现”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: 扩展tk.mybatis的流式查询功能如何实现
本文链接: https://lsjlt.com/news/301025.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0