扩展tk.mybatis的流式查询功能实现
程序员文章站
2022-03-09 08:21:24
mybatis查询默认是一次获取全部, 有时候需要查询上万上百万数据时,如果一次性读取到内存中,会容易导致oom问题。这时候需要采用流式查询。以下扩展了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的流式查询功能实现的文章就介绍到这了,更多相关tk.mybatis 流式查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Python全栈之文件函数和函数参数