Mybatis Plus中的流式查詢(xún)案例
Mybatis Plus流式查詢(xún)
mybatis plus 中自定義如下接口,就可以實(shí)現(xiàn)流式查詢(xú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);通用流式查詢(xún)
編寫(xiě)流式查詢(xún)的方法:
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 寫(xiě)入下方的 method 即可使用。
void fetchByStream(@Param(Constants.WRAPPER) Wrapper<T> wrapper, ResultHandler<T> handler);
Mybatis Plus大數(shù)據(jù)量流式查詢(xún)
一、在需要使用流式查詢(xún)的mapper文件中,定義流式查詢(xún)方法
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ù)庫(kù)訪問(wèn)層
?*
?* @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();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //用來(lái)完成最后一批數(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();
? ? ? ? }
? ? }
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決mybatis-plus動(dòng)態(tài)數(shù)據(jù)源切換不生效的問(wèn)題
本文主要介紹了解決mybatis-plus動(dòng)態(tài)數(shù)據(jù)源切換不生效的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
SpringBoot結(jié)合Neo4j自定義cypherSql的方法
這篇文章主要介紹了SpringBoot結(jié)合Neo4j自定義cypherSql,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
Spring中DAO被循環(huán)調(diào)用的時(shí)候數(shù)據(jù)不實(shí)時(shí)更新的解決方法
這篇文章主要介紹了Spring中DAO被循環(huán)調(diào)用的時(shí)候數(shù)據(jù)不實(shí)時(shí)更新的解決方法,需要的朋友可以參考下2014-08-08
Java?Cookie與Session實(shí)現(xiàn)會(huì)話(huà)跟蹤詳解
session的工作原理和cookie非常類(lèi)似,在cookie中存放一個(gè)sessionID,真實(shí)的數(shù)據(jù)存放在服務(wù)器端,客戶(hù)端每次發(fā)送請(qǐng)求的時(shí)候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2022-11-11
java字節(jié)流、字符流與轉(zhuǎn)換流過(guò)程
輸入輸出流(IO流)是數(shù)據(jù)傳輸?shù)某橄蟾拍?用于表示數(shù)據(jù)在設(shè)備間的傳輸過(guò)程,IO流按數(shù)據(jù)類(lèi)型分為字符流和字節(jié)流,按數(shù)據(jù)流向分為輸入流和輸出流,字節(jié)流操作單個(gè)字節(jié),字符流操作字符,在實(shí)際應(yīng)用中,非文本文件多用字節(jié)流操作2024-10-10
springboot實(shí)現(xiàn)攔截器之驗(yàn)證登錄示例
本篇文章主要介紹了springboot實(shí)現(xiàn)攔截器之驗(yàn)證登錄示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫(xiě)工具
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫(xiě)工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

