1、场景:查询大量数据,导出大量数据等,防止内存溢出
2、示例:Cursor和List类似
import org.apache.ibatis.cursor.Cursor;
Cursor<FundinfoEntity> fundinfos = fundInfoMapper.cursorAllFundInfo();
for (FundinfoEntity fundinfo : fundinfos) {
FundinfoEntity o32FundInfo = ottMapper.queryO32FundInfo(fundinfo.getO32FundId(),cpicSchemaConfiguration.getO32());
if (ObjectUtils.isEmpty(o32FundInfo)){
fundInfoMapper.updateCompareStatus(fundinfo.getO32FundId(),
O32CompareStatusEnum.NOEXIST.getKey());
}else{
String status = Objects.equals(fundinfo, o32FundInfo) ? O32CompareStatusEnum.SAME.getDescrible() :
O32CompareStatusEnum.DIFF.getKey();
fundInfoMapper.updateCompareStatus(fundinfo.getO32FundId(),status);
}
}
3、遇到的问题:java.lang.IllegalStateException: A Cursor is already closed.
由于Cursor是一条条查,所以会关闭会话,需要在方法上加@Transactional即可