..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mybatis-plus查詢源碼的示例分析

發(fā)布時間:2022-03-02 14:33:28 來源:億速云 閱讀:294 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹mybatis-plus查詢源碼的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

配置詳情

pom.xml

dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.4.1</version>
 </dependency>

mapper

public interface GenTableMapper extends BaseMapper<GenTable> {
}

測試類

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GendemoApplication.class)
public class BlockqueueTestDemo {
    @Autowired
    GenTableMapper genTableMapper;
    @Test
    public void test(){
        List<GenTable> genTables = 
        genTableMapper.selectList(new QueryWrapper<>());
    }
}

debug流程

1.發(fā)現(xiàn) genTableMapper 是一個代理對象類型。

mybatis-plus查詢源碼的示例分析

2.進(jìn)入代理對象MybatisMapperProxy , 調(diào)用其invoke 方法,方法的Class類型為BaseMapper.selectList()

mybatis-plus查詢源碼的示例分析

3.其中cachedInvoker()方法會返回一個PlainMethodInvoker ,它重寫了MapperMethodInvoker 接口的invoke()方法

mybatis-plus查詢源碼的示例分析

4.最終會調(diào)用MybatisMapperMethodexecute()方法

mybatis-plus查詢源碼的示例分析

public class MybatisMapperMethod {
    public Object execute(SqlSession sqlSession, Object[] args) {
        Object result;
        switch (command.getType()) {
            case INSERT: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.insert(command.getName(), param));
                break;
            }
            case UPDATE: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.update(command.getName(), param));
                break;
            }
            case DELETE: {
                Object param = method.convertArgsToSqlCommandParam(args);
                result = rowCountResult(sqlSession.delete(command.getName(), param));
                break;
            }
            case SELECT:
                if (method.returnsVoid() && method.hasResultHandler()) {
                    executeWithResultHandler(sqlSession, args);
                    result = null;
                } else if (method.returnsMany()) {
                    result = executeForMany(sqlSession, args);
                } else if (method.returnsMap()) {
                    result = executeForMap(sqlSession, args);
                } else if (method.returnsCursor()) {
                    result = executeForCursor(sqlSession, args);
                } else {
                    Object param = method.convertArgsToSqlCommandParam(args);
                    // TODO 這里下面改了
                    if (IPage.class.isAssignableFrom(method.getReturnType())) {
                        result = executeForIPage(sqlSession, args);
                        // TODO 這里上面改了
                    } else {
                        result = sqlSession.selectOne(command.getName(), param);
                        if (method.returnsOptional()
                            && (result == null || !method.getReturnType().equals(result.getClass()))) {
                            result = Optional.ofNullable(result);
                        }
                    }
                }
                break;
            case FLUSH:
                result = sqlSession.flushStatements();
                break;
            default:
                throw new BindingException("Unknown execution method for: " + command.getName());
        }
        if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
            throw new BindingException("Mapper method '" + command.getName()
                + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
        }
        return result;
    }
}

5.這是經(jīng)過判斷會進(jìn)入executeForMany(sqlSession, args)方法,此時方法和參數(shù)都顯示出來了。sqlSession的類型是SqlSessionTemplate , 為什么要注意這個 sqlSession的類型?因為SqlSession是一個接口,有很多實現(xiàn)類,有時候我們并不知道到底調(diào)用了哪個實現(xiàn)類的selectList()方法,這個時候我們看類型就知道了,就可以進(jìn)入SqlSessionTemplate類,找到selectList()打上斷點,debug就過來了。

mybatis-plus查詢源碼的示例分析

6.利用同樣的方法,又調(diào)用了DefaultSqlSessionselectList()方法。

mybatis-plus查詢源碼的示例分析


7.來到DefaultSqlSessionselectList() 方法中,此時已經(jīng)進(jìn)入到mybatis的源碼范圍了。executor的類型是MybatisCachingExecutor

mybatis-plus查詢源碼的示例分析

8.此時要注意MybatisCachingExecutor 代理類的handler是一個Plugin

mybatis-plus查詢源碼的示例分析

mybatis-plus查詢源碼的示例分析

9.因為我使用到了分頁插件,所以會來到com.github.pagehelperPageInterceptor

mybatis-plus查詢源碼的示例分析

10.由MybatisCachingExecutor來執(zhí)行查詢

mybatis-plus查詢源碼的示例分析

11.MybatisCachingExecutor 委派 BaseExecutor 執(zhí)行查詢

mybatis-plus查詢源碼的示例分析

mybatis-plus查詢源碼的示例分析

12.最終委派到PreparedStatementHandler來處理

mybatis-plus查詢源碼的示例分析

13.最后由DefaultResultSetHandler來封裝結(jié)果集

@Override
  public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

    final List<Object> multipleResults = new ArrayList<>();
    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
      ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }
    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {
      while (rsw != null && resultSetCount < resultSets.length) {
        ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
        if (parentMapping != null) {
          String nestedResultMapId = parentMapping.getNestedResultMapId();
          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
          handleResultSet(rsw, resultMap, null, parentMapping);
        }
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
      }
    return collapseSingleResultList(multipleResults);
  }

以上是“mybatis-plus查詢源碼的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI