您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“MybatisPlus中的流式查詢?cè)趺词褂谩?,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“MybatisPlus中的流式查詢?cè)趺词褂谩卑?
mybatis plus 中自定義如下接口,就可以實(shí)現(xiàn)流式查詢,mybatis 中同樣適用。
@Select("select * from t_xxx t ${ew.customSqlSegment}") @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000) @ResultType(ClearReconDiffAbnormalDO.class) void listByStream(@Param(Constants.WRAPPER) Wrapper<Model> wrapper, ResultHandler<Model> resultHandler);
編寫流式查詢的方法:
public class FetchByStream extends AbstractMethod { private static final String METHOD = "fetchByStream"; @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { String sqlFormat = "<script>\nSELECT %s FROM %s %s %s\n</script>"; String sql = String.format(sqlFormat, sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); String statementName = mapperClass.getName() + DOT + METHOD; if (configuration.hasStatement(statementName, false)) { logger.warn(LEFT_SQ_BRACKET + statementName + "] Has been loaded by XML or SqlProvider or Mybatis's Annotation, so ignoring this injection for [" + getClass() + RIGHT_SQ_BRACKET); return null; } /* 緩存邏輯處理 */ return builderAssistant.addMappedStatement(METHOD, sqlSource, StatementType.PREPARED, SqlCommandType.SELECT, Integer.MIN_VALUE, null, null, null, null, modelClass, ResultSetType.FORWARD_ONLY, true, true, false, null, null, null, configuration.getDatabaseId(), languageDriver, null); } }
然后再注入通用方法,在Mapper 寫入下方的 method 即可使用。
void fetchByStream(@Param(Constants.WRAPPER) Wrapper<T> wrapper, ResultHandler<T> handler);
一、在需要使用流式查詢的mapper文件中,定義流式查詢方法
package com.unionpay.dao.db2; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.unionpay.entity.TblMallOrder; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.session.ResultHandler; /** * (TblMallOrder)表數(shù)據(jù)庫訪問層 * * @author liudong * @since 2020-09-15 17:07:13 */ @Mapper public interface TblMallOrderDao extends BaseMapper<TblMallOrder> { @Select("${sql}") @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000) @ResultType(TblMallOrder.class) void dynamicSelectLargeData1(@Param("sql") String sql, ResultHandler<TblMallOrder> handler); @Select("${sql}") @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000) @ResultType(Map.class) void dynamicSelectLargeData2(@Param("sql") String sql, ResultHandler<Map> handler); }
二、使用示例
@RestController public class TestSearchLargeData { // 這是每批處理的大小 private final static int BATCH_SIZE = 1000; private int size; // 存儲(chǔ)每批數(shù)據(jù)的臨時(shí)容器 private List<TblMallOrder> mallOrders; @Autowired private TblMallOrderDao tblMallOrderDao; @GetMapping("/getLargeData1") public void getLargeData1() { String sql = "select * from t_mall_order"; tblMallOrderDao.dynamicSelectLargeData1(sql, new ResultHandler<TblMallOrder>() { @Override public void handleResult(ResultContext<? extends TblMallOrder> resultContext) { TblMallOrder tblMallOrder = resultContext.getResultObject(); System.out.println(tblMallOrder); } }); } @GetMapping("/getLargeData2") public void getLargeData2() { String sql = "select * from t_mall_order"; tblMallOrderDao.dynamicSelectLargeData1(sql, new ResultHandler<TblMallOrder>() { @Override public void handleResult(ResultContext<? extends TblMallOrder> resultContext) { TblMallOrder tblMallOrder = resultContext.getResultObject(); System.out.println(tblMallOrder); // 你可以看自己的項(xiàng)目需要分批進(jìn)行處理或者單個(gè)處理,這里以分批處理為例 mallOrders.add(tblMallOrder); size++; if (size == BATCH_SIZE) { handle(); } } }); //用來完成最后一批數(shù)據(jù)處理 handle(); } /** * 數(shù)據(jù)處理 */ private void handle(){ try{ // 在這里可以對(duì)你獲取到的批量結(jié)果數(shù)據(jù)進(jìn)行需要的業(yè)務(wù)處理 }catch (Exception e){ e.printStackTrace(); }finally { // 處理完每批數(shù)據(jù)后后將臨時(shí)清空 size = 0; mallOrders.clear(); } } }
到此,相信大家對(duì)“MybatisPlus中的流式查詢?cè)趺词褂谩庇辛烁畹牧私?,不妨來?shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。