ResultSetHandler是mybatis的關鍵類之一,用於對jdbc返回的ResultSet進行映射處理,其中包括列前綴處理,邏輯分頁,鑑別器(Discriminator,基於值實現動態映射列)處理等等。html
ResultSetHandler在StatementHandler執行過程當中構建,以下:apache
接下去來看ResultSetHandler的定義。最主要的是handleResultSets,它負責普通查詢結果的處理。session
public interface ResultSetHandler { <E> List<E> handleResultSets(Statement var1) throws SQLException; <E> Cursor<E> handleCursorResultSets(Statement var1) throws SQLException; void handleOutputParameters(CallableStatement var1) throws SQLException; }
handleResultSets在StatementHandler執行完成後被調用,以下:
要對結果集自定義處理的話,能夠改動此處源碼。由於mybatis攔截器支持對四大對象(Executor,StatementHandler,ParameterHandler,ResultSetHandler)都支持,因此也能夠經過攔截器(參見mybatis自定義插件開發詳解)實現,各有利弊,LZ採用後者。mybatis
邏輯分頁也是在handleResultSets中處理。邏輯分頁由org.apache.ibatis.session.RowBounds實現,mybatis內置的應用層邏輯分頁實現定義,但基本上不多使用,定義以下。app
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.apache.ibatis.session; public class RowBounds { public static final int NO_ROW_OFFSET = 0; public static final int NO_ROW_LIMIT = 2147483647; public static final RowBounds DEFAULT = new RowBounds(); private final int offset; private final int limit; public RowBounds() { this.offset = 0; this.limit = 2147483647; } public RowBounds(int offset, int limit) { this.offset = offset; this.limit = limit; } public int getOffset() { return this.offset; } public int getLimit() { return this.limit; } }
默認值爲不分頁。在shouldProcessMoreRows中判斷,以下:post
private boolean shouldProcessMoreRows(ResultContext<?> context, RowBounds rowBounds) throws SQLException { return !context.isStopped() && context.getResultCount() < rowBounds.getLimit(); }
在處理每一行記錄的時候,先處理沒有明確映射(無或不在ResultMap中)的屬性,而後處理明確映射,以下:性能
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException { ResultLoaderMap lazyLoader = new ResultLoaderMap();
// 建立目標類型對象,如Pojo或Map Object rowValue = this.createResultObject(rsw, resultMap, lazyLoader, (String)null); if (rowValue != null && !this.hasTypeHandlerForResultObject(rsw, resultMap.getType())) { MetaObject metaObject = this.configuration.newMetaObject(rowValue); boolean foundValues = this.useConstructorMappings; if (this.shouldApplyAutomaticMappings(resultMap, false)) { // map就是在這裏自動映射的 foundValues = this.applyAutomaticMappings(rsw, resultMap, metaObject, (String)null) || foundValues; } foundValues = this.applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, (String)null) || foundValues; foundValues = lazyLoader.size() > 0 || foundValues; rowValue = !foundValues && !this.configuration.isReturnInstanceForEmptyRow() ? null : rowValue; } return rowValue; }
接下去再來看ResultHandler,它用來對每行記錄映射或處理,典型的好比二次過濾或脫敏處理,均可以在這裏處理,固然也能夠經過插件,只不過在這裏處理從性能上看最佳。其定義比較簡單:this
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.apache.ibatis.session; public interface ResultHandler<T> { void handleResult(ResultContext<? extends T> var1); }
相對來講,ResultHandler的實現很簡單,這裏就不詳解了,真的想了解的能夠參考https://www.cnblogs.com/51life/p/9633002.html。url