Mybatis Plus中的流式查詢案例
更新時間:2022年08月17日 14:41:34 作者:Jon Kee
這篇文章主要介紹了Mybatis Plus中的流式查詢案例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Mybatis Plus流式查詢
mybatis plus 中自定義如下接口,就可以實現(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);
Mybatis Plus大數(shù)據(jù)量流式查詢
一、在需要使用流式查詢的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;
? ? // 存儲每批數(shù)據(jù)的臨時容器
? ? 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);
? ? ? ? ? ? ? ? // 你可以看自己的項目需要分批進行處理或者單個處理,這里以分批處理為例
? ? ? ? ? ? ? ? mallOrders.add(tblMallOrder);
? ? ? ? ? ? ? ? size++;
? ? ? ? ? ? ? ? if (size == BATCH_SIZE) {
? ? ? ? ? ? ? ? ? ? handle();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //用來完成最后一批數(shù)據(jù)處理
? ? ? ? handle();
? ? }
? ? /**
? ? ?* 數(shù)據(jù)處理
? ? ?*/
? ? private void handle(){
? ? ? ? try{
? ? ? ? ? ? // 在這里可以對你獲取到的批量結(jié)果數(shù)據(jù)進行需要的業(yè)務處理
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }finally {
? ? ? ? ? ? // 處理完每批數(shù)據(jù)后后將臨時清空
? ? ? ? ? ? size = 0;
? ? ? ? ? ? mallOrders.clear();
? ? ? ? }
? ? }
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
解決mybatis-plus動態(tài)數(shù)據(jù)源切換不生效的問題
本文主要介紹了解決mybatis-plus動態(tài)數(shù)據(jù)源切換不生效的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
SpringBoot結(jié)合Neo4j自定義cypherSql的方法
這篇文章主要介紹了SpringBoot結(jié)合Neo4j自定義cypherSql,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
Spring中DAO被循環(huán)調(diào)用的時候數(shù)據(jù)不實時更新的解決方法
這篇文章主要介紹了Spring中DAO被循環(huán)調(diào)用的時候數(shù)據(jù)不實時更新的解決方法,需要的朋友可以參考下2014-08-08
Java?Cookie與Session實現(xiàn)會話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務端根據(jù)sessionID進行數(shù)據(jù)的響應2022-11-11
java實現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具
這篇文章主要為大家詳細介紹了java實現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04

