微前端架构如何改变企业的开发模式与效率提升
1107
2022-11-18
扩展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
* @author sunchangtan
*/
@tk.mybatis.mapper.annotation.RegisterMapper
public interface SelectStreamByExampleMapper
/**
* 根据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
* @author sunchangtan
*/
@tk.mybatis.mapper.annotation.RegisterMapper
public interface SelectStreamByExampleRowBoundsMapper /** * 根据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 * @author sunchangtan */ @tk.mybatis.mapper.annotation.RegisterMapper public interface StreamaRlnjvqmMapper SelectStreamByExampleMapper SelectStreamByExampleRowBoundsMapper } 在BaseMapper中加http://入StreamMapper /** * Mapper的基类 * @author sunchangtan * @param */ public interface BaseMapper } 使用例子: 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); });
/**
* 根据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
* @author sunchangtan
*/
@tk.mybatis.mapper.annotation.RegisterMapper
public interface StreamaRlnjvqmMapper
SelectStreamByExampleMapper
SelectStreamByExampleRowBoundsMapper
}
在BaseMapper中加http://入StreamMapper
/**
* Mapper的基类
* @author sunchangtan
* @param
*/
public interface BaseMapper
}
使用例子:
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);
});
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~