Mybatis的執行器
下面先展示一張圖,用來說明一下Mybatis執行器的整體架構

SimpleExecutor
首先SimpleExecutor是我們最常使用的一個執行器,無論我們執行什麼方法預設調用的都是SimpleExecutor
下面是基本使用,這裡可能會比較懵了,哪裡來的configuration,doQuery,RowBounds,ResultHandler,BoundSql
在這裡我來一一解釋
SimpleExecutor simpleExecutor = new SimpleExecutor(configuration, transaction);
MappedStatement ms = configuration.getMappedStatement("com.guan.ibatis.mapper.UserMapper.queryUsersInfo");
BoundSql boundSql = ms.getBoundSql(null);
List<User> users = simpleExecutor.doQuery(ms, null,
RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER, boundSql);
users.forEach(System.out::println);
- configuration我們讀取配置檔案使用SqlSessionFactoryBuilder來建構,而配置檔案(Mybatis-config.xml)解析後就會将解析完的所有資料放到一個名為Configuration的類裡面,我們的一些操作,比如設定Setting,設定資料源,設定映射檔案,都可以通過
來進行配置,而擷取Configuration的實力,隻需要我們SqlSessionFactoryBuilder.build()所建立的SqlSessionFactory就可以擷取Configuration了--->new Configuration()
SqlSessionFactory.getConfiguration()
- doQuery是BaseMapper的一個抽象方法,分别由三個子類進行實作,是最基本的查詢方法,無論調用什麼查詢方法都會調用doQuery這個方法
- RowBounds分頁條件,我們可以new RowBounds()來自定義分頁條件,而RowBounds.DEFAULT就是new一個0-Integer.MAX_VALUE的RowBounds
- ResultHandler結果處理器
- BoundSql我們編寫的sql語句,擷取方法:
沒有參數就可以傳nullms.getBoundSql()
- ms就是MappedStatement擷取我們對應方法的屬性,參數為statementid(包名.類名.方法名)