mybatis查询默认是一次获取全部, 有时候需要查询上万上百万数据时,如果一次性读取到内存中,会容易导致oom问题。这时候需要采用流式查询。以下扩展了tk.mybatis的流式查询功能。 直接上干货:
@options注解是关键
import org.apache.ibatis.annotations.options;import org.apache.ibatis.annotations.lectprovider;import org.apache.ibatis.mapping.resultttype;import org.apache.ibatis.ssion.result年的英文handler;/** * 通用mapper接口,流式查询 * * @param <t> 不能为空 * @author sunchangtan */@tk.mybatis.mapper.annotation.registermapperpublic interface lectstreamby传说故事examplemapper<t> { /** * 根据example条件和rowbounds进行流式查询 * * @param example * @return */ @options(resultttype = resultttype.forward_only, fetchsize = 1000) @lectprovider(type = streamexampleprovider.class, method = "dynamicsql") void lectstreambyexamplemapper(object example, resulthandler resulthandler);}
带rowbounds的流式查询
import org.apache.ibatis.annotations.options;import org.apache.ibatis.annotations.lectprovider;import org.apache.ibatis.mapping.resultttype;import org.apache.ibatis.ssion.resulthandler;import org.apache.ibat李白的诗is.ssion.rowbounds;/** * 通用mapper接口,流式查询 * * @param <t> 不能为空 * @author sunchangtan */@tk.mybatis.mapper.annotation.registermapperpublic interface lectstreambyexamplerowboundsmapper<t> { /** * 根据example条件和rowbounds进行流式查询 * * @param example * @return */ @options(resultttype = resultttype.forward_only, fetchsize = 1000) @lectprovider(type = streamexampleprovider.class, method = "dynamicsql") void lectstreambyexamplerowboundsmapper(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 lectstreambyexamplemapper(mappedstatement ms) { return this.lectbyexample(ms); } /** * 根据example和rowbounds流式查询 * @param ms * @return */ public string lectstreambyexamplerowboun爱就一个字歌词dsmapper(mappedstatement ms) { return this.lectbyexample(ms); }}
将lectstreambyexamplemapper和lectstreambyexamplerowboundsmapper组合成一个接口
/** * 流式查询接口 * @param <t> * @author sunchangtan */@tk.mybatis.mapper.annotation.registermapperpublic interface streammapper<t> extends lectstreambyexamplemapper<t>, lectstreambyexamplerowboundsmapper<t> {}
在bamapper中加入streammapper
/** * mapper的基类 * @author sunchangtan * @param <t> */public interface bamapper<t> extends mapper<t>, mysqlmapper<t>, streammapper<t> {}
使用例子:
this.urmapper.lectstreambyexamplerowbou假期ndsmapper(example, new rowbounds(0, 1), resultcontext -> { ur ur= (ur) resultcontext.getresultobject(); system.out.println(ur); });this.urmapper.lectstreambyexamplemapper(example, resultcontext -> { ur ur= (ur) resultcontext.getresultobject(); system.out.println(ur);});
到此这篇关于扩展tk.mybatis的流式查询功能实现的文章就介绍到这了,更多相关tk.mybatis 流式查询内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-03 23:31:11,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ba1b0e1e6fe424440bad2b1e5fd385ea.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:扩展tk.mybatis的流式查询功能实现.doc
本文 PDF 下载地址:扩展tk.mybatis的流式查询功能实现.pdf
留言与评论(共有 0 条评论) |