這篇文章主要講解了“Mybatis游標查詢大量數據的方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Mybatis游標查詢大量數據的方法是什么”吧!
對大量數據進行處理時,為防止內存泄漏情況發生,所以采用mybatis plus游標方式進行數據查詢處理,當查詢百萬級的數據的時候,使用游標可以節省內存的消耗,不需要一次性取出所有數據,可以進行逐條處理或逐條取出部分批量處理
使用Cursor類型進行數據接收
@Options,fetchSize設置為Integer最小值
@Select,寫查詢sql
@Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE)
@Select("select domain from illegal_domain where icpstatus != #{icpstatus}")
Cursor<IllegalDomain> getDayJobDomain(@Param("icpstatus") Integer icpstatus);Cursor<IllegalDomain> domainList = illegalDomainMapper.getDayJobDomain(1);
數據處理
forEach方式
domainList.forEach(illegalDomain -> {
//處理邏輯,根據業務需求自行完成
Future<IcpStatusVo> future = checkIcpThreadPool.submit(new IcpCheckThread(illegalDomain.getDomain(), configMap));
results.add(future);
});迭代器
Iterator<IllegalDomain> iter = domainList.iterator();
while (iter.hasNext()) {
<!--// Fetch next 10 employees-->
<!--for(int i = 0; i<10 && iter.hasNext(); i++) {-->
<!-- smallChunk.add(iter.next());-->
<!--}-->
//處理邏輯,根據業務需求自行完成
Future<IcpStatusVo> future = checkIcpThreadPool.submit(new IcpCheckThread(illegalDomain.getDomain(), configMap));
results.add(future);
}使用完畢后,在finally塊釋放資源,否則游標不關閉也可能會導致內存溢出問題
try{
//your code
} catch (Exception e) {
log.error(e);
} finally {
if(null != domainList){
try {
domainList.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}當查詢百萬級的數據的時候,查詢出所有數據并放入內存中時會發生OOM(OutOfMemoryException),使用游標可以節省內存的消耗,不需要一次性取出所有數據,可以進行逐條處理或逐條取出部分批量處理,在此場景下就可以使用游標的概念來解決這個問題。
游標(Cursor)是處理數據的一種方法,為了查看或者處理結果集中的數據,游標提供了在結果集中一次一行或者多行前進或向后瀏覽數據的能力。
Demo:
// 第一種
@Options(resultSetType = ResultSetType.FORWARD_ONLY)
@Select("SELECT * FROM department WHERE status = 0")
List<DepartmentEntity> queryDepartmentAll();
// 第二種 在Mybatis-3.4.0版本中,不支持@select注解,在3.4.1版本中已經修復:
@Options(resultSetType = ResultSetType.FORWARD_ONLY)
@Select("SELECT * FROM department WHERE status = 0")
Cursor<Employee> cursorQueryDepartmentAll();@Service
public class DepartmentServiceImpl implements DepartmentService {
private static final Logger LOG = LoggerFactory.getLogger(DepartmentServiceImpl.class);
@Autowired
private DepartmentDao departmentDao;
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public List<DepartmentDTO> getDepartmentList(DepartmentSearchParam param) {
Cursor<Object> cursor = null;
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession();
cursor = sqlSession.selectCursor(DepartmentDao.class.getName() + ".queryDepartmentAll");
cursor.forEach(e -> {
// 處理邏輯
});
// 也可以使用迭代器:Iterator<Object> iterator = cursor.iterator();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != cursor) {
try {
cursor.close();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
if (null != sqlSession) {
try {
sqlSession.close();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
}
}
}ResultSet.TYPE_FORWORD_ONLY 結果集的游標只能向下滾動。
ResultSet.TYPE_SCROLL_INSENSITIVE 結果集的游標可以上下移動,當數據庫變化時,當前結果集不變。
ResultSet.TYPE_SCROLL_SENSITIVE 返回可滾動的結果集,當數據庫變化時,當前結果集同步改變。
注意:游標是可以前后移動的。如果resultSetType = TYPE_SCROLL_INSENSITIVE ,就是設置游標就可以前后移動。
Mybatis為了保證可以前后移動,Mybatis會把之前查詢的數據一直保存在內存中。
所以并不能根本解決OOM,所以我們這里需要設置為@Options(resultSetType = ResultSetType.FORWARD_ONLY)(其實默認就是ResultSetType.FORWARD_ONLY)
感謝各位的閱讀,以上就是“Mybatis游標查詢大量數據的方法是什么”的內容了,經過本文的學習后,相信大家對Mybatis游標查詢大量數據的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。