mybatis ResultHandler vs ResultSetHandler custom extensions and custom plug-in development explain mybatis

ResultSetHandler mybatis class is one of the key for the ResultSet returned jdbc mapping processing, including the processing column prefix, logical page, discriminator (Discriminator, a value based on a dynamic mapping column) processing and the like.

ResultSetHandler StatementHandler built during execution, as follows:

 

Next look at the definition of ResultSetHandler. The main thing is handleResultSets, which is responsible for general processing query results.

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 execution after being StatementHandler complete the call, as follows:

 To customize a result set processing, you can change the source code here. Because mybatis interceptors are supported on the four support objects (Executor, StatementHandler, ParameterHandler, ResultSetHandler ), it is also possible via the interceptor (see mybatis custom plug-in development explain ) implemented, have advantages and disadvantages, LZ latter.

HandleResultSets logical page is in the process. Implemented by a logical page org.apache.ibatis.session.RowBounds, it mybatis built logical page implementation defined application layer, but very seldom used, as defined below.

//
// 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;
    }
}

The default value is not paged. ShouldProcessMoreRows determined as follows:

    private boolean shouldProcessMoreRows(ResultContext<?> context, RowBounds rowBounds) throws SQLException {
        return !context.isStopped() && context.getResultCount() < rowBounds.getLimit();
    }

 When the processing of each row, the first process is not explicitly mapped attributes (none or not in ResultMap), and the processing explicitly mapped, as follows:

    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; }

 Next look at the ResultHandler, which is used to record or mapping process for each line, a typical example of the secondary filter or desensitization treatment, can be processed here, of course, can also plug-in, but where the processing from performance point of view the most good. Its definition is simple:

//
// 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);
}

 Relatively speaking, ResultHandler implementation is very simple, here is not to explain, really want to know can refer https://www.cnblogs.com/51life/p/9633002.html.

Guess you like

Origin www.cnblogs.com/zhjh256/p/11516881.html