亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis-plus查詢源碼詳解

 更新時間:2022年03月02日 11:54:07   作者:這個世界太瘋狂了  
這篇文章主要介紹了mybatis-plus查詢源碼解讀,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

配置詳情

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ā)現 genTableMapper 是一個代理對象類型。

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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.這是經過判斷會進入executeForMany(sqlSession, args)方法,此時方法和參數都顯示出來了。sqlSession的類型是SqlSessionTemplate , 為什么要注意這個 sqlSession的類型?因為SqlSession是一個接口,有很多實現類,有時候我們并不知道到底調用了哪個實現類的selectList()方法,這個時候我們看類型就知道了,就可以進入SqlSessionTemplate類,找到selectList()打上斷點,debug就過來了。

在這里插入圖片描述

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

在這里插入圖片描述


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

在這里插入圖片描述

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

在這里插入圖片描述

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

在這里插入圖片描述

12.最終委派到PreparedStatementHandler來處理

在這里插入圖片描述

13.最后由DefaultResultSetHandler來封裝結果集

@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 的自動配置過程,有興趣的可以持續(xù)關注一下。
文中有錯誤的地方不吝賜教謝謝。

到此這篇關于mybatis-plus查詢源碼走讀的文章就介紹到這了,更多相關mybatis-plus查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • springboot+redis實現微博熱搜排行榜的示例代碼

    springboot+redis實現微博熱搜排行榜的示例代碼

    本文主要介紹了springboot+redis實現微博熱搜排行榜的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • Java進階知識之反射的概念與獲取方法

    Java進階知識之反射的概念與獲取方法

    這篇文章主要給大家介紹了關于Java進階知識之反射的概念與獲取方法的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • MyBatis配置文件解析與MyBatis實例演示

    MyBatis配置文件解析與MyBatis實例演示

    這篇文章主要介紹了MyBatis配置文件解析與MyBatis實例演示以及怎樣編譯安裝MyBatis,需要的朋友可以參考下
    2022-04-04
  • Springboot日志開啟SLF4J過程解析

    Springboot日志開啟SLF4J過程解析

    這篇文章主要介紹了Springboot日志開啟SLF4J過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • JAVA 根據Url把多文件打包成ZIP下載實例

    JAVA 根據Url把多文件打包成ZIP下載實例

    這篇文章主要介紹了JAVA 根據Url把多文件打包成ZIP下載的相關資料,需要的朋友可以參考下
    2017-08-08
  • Spring.Net控制反轉IoC入門使用

    Spring.Net控制反轉IoC入門使用

    這篇文章主要為大家詳細介紹了Spring.Net控制反轉IoC入門使用的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Spring boot 配置多個redis的方法示例

    Spring boot 配置多個redis的方法示例

    這篇文章主要介紹了Spring boot 配置多個redis的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Springboot中yml文件不生效原因分析及解決

    Springboot中yml文件不生效原因分析及解決

    這篇文章給大家介紹了Springboot中yml文件不生效原因分析及解決方法,通過圖文給大家講解的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • Mybatis實現自定義類型轉換器TypeHandler的方法

    Mybatis實現自定義類型轉換器TypeHandler的方法

    Mybatis實現自定義的轉換器非常的簡單,只需要三步就可以實現自定義類型轉換器TypeHandler,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-07-07
  • SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實現

    SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實現

    這篇文章主要介紹了SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09

最新評論