Mybatis?SqlSession案例詳解
前言
老規(guī)矩,先上案例代碼,我們按照這個(gè)案例一步一步的搞定Mybatis源碼。
public class MybatisApplication { public static final String URL = "jdbc:mysql://localhost:3306/mblog"; public static final String USER = "root"; public static final String PASSWORD = "123456"; public static void main(String[] args) { String resource = "mybatis-config.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(userMapper.selectById(1)); } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } sqlSession.close(); } } }
前面我們已經(jīng)講了Mybatis是如何解析相關(guān)配置文件的,如果怕迷路,還是建議先看前一篇文章:
繼續(xù)開擼~~
SqlSession sqlSession = sqlSessionFactory.openSession();
前面那篇文章已經(jīng)分析了,這里的sqlSessionFactory其實(shí)就是DefaultSqlSessionFactory。
所以這里,我們就從DefaultSqlSessionFactory里的openSession方法開始。
public class DefaultSqlSessionFactory implements SqlSessionFactory { private final Configuration configuration; public DefaultSqlSessionFactory(Configuration configuration) { this.configuration = configuration; } //創(chuàng)建session,這個(gè)方法直接調(diào)用本類中的另外一個(gè)方法 @Override public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); } //其實(shí)是調(diào)用這個(gè)方法 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { //對應(yīng)xml標(biāo)簽<environments> ,這個(gè)在配置文件解析的時(shí)候就已經(jīng)存放到configuration中了。 final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //創(chuàng)建一個(gè)executor來執(zhí)行SQL final Executor executor = configuration.newExecutor(tx, execType); //這里也說明了,為什么我們代碼里的SqlSession是DefaultSqlSession return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } } private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) { if (environment == null || environment.getTransactionFactory() == null) { return new ManagedTransactionFactory(); } return environment.getTransactionFactory(); } }
這個(gè)方法中的主要內(nèi)容有:
下面我們就來逐個(gè)攻破。
創(chuàng)建事務(wù)Transaction
事務(wù)工廠類型可以配置為JDBC類型或者M(jìn)ANAGED類型。
JdbcTransactionFactory生產(chǎn)JdbcTransaction。
ManagedTransactionFactory生產(chǎn)ManagedTransaction。
如果配置的JDBC,則會使用Connection對象的commit()、rollback()、close()方法來管理事務(wù)。
如果我們配置的是MANAGED,會把事務(wù)交給容器來管理,比如JBOSS,Weblogic。因?yàn)槲覀兪潜镜嘏艿某绦?,如果配置成MANAGED就會不有任何事務(wù)。
但是,如果我們項(xiàng)目中是Spring集成Mybatis,則沒有必要配置事務(wù),因?yàn)槲覀儠苯釉赼pplicationContext.xml里配置數(shù)據(jù)源和事務(wù)管理器,從而覆蓋Mybatis的配置。
創(chuàng)建執(zhí)行器Executor
調(diào)用configuration的newExecutor
方法創(chuàng)建Executor。
final Executor executor = configuration.newExecutor(tx, execType); //Configuration中 public Executor newExecutor(Transaction transaction, ExecutorType executorType) { executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor executor; //第一步 if (ExecutorType.BATCH == executorType) { executor = new BatchExecutor(this, transaction); } else if (ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this, transaction); } else { executor = new SimpleExecutor(this, transaction); } //第二步 if (cacheEnabled) { executor = new CachingExecutor(executor); } //第三步 executor = (Executor) interceptorChain.pluginAll(executor); return executor; }
此方法分三個(gè)步驟。
第一步:創(chuàng)建執(zhí)行器
Executor的基本類型有三種:
public enum ExecutorType { SIMPLE, REUSE, BATCH }
SIMPLE為默認(rèn)類型。
為什么要讓抽象類BaseExecutor實(shí)現(xiàn)Executor接口,然后讓具體實(shí)現(xiàn)類繼承抽象類呢?
這就是模板方法模式的實(shí)現(xiàn)。
模板方法模式就是定義一個(gè)算法骨架,并允許子類為一個(gè)或者多個(gè)步驟提供實(shí)現(xiàn)。模板方法是得子類可以再不改變算法結(jié)構(gòu)的情況下,重新定義算法的某些步驟。
關(guān)于模板方法模式推薦閱讀:
抽象方法是在子類匯總實(shí)現(xiàn)的,每種執(zhí)行器自己實(shí)現(xiàn)自己的邏輯,BaseExecutor最終會調(diào)用到具體的子類中。
抽象方法
protected abstract int doUpdate(MappedStatement ms, Object parameter) throws SQLException; protected abstract List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException; protected abstract <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException; protected abstract <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException;
第二步:緩存裝飾
在上面代碼中的第二步
if (cacheEnabled) { executor = new CachingExecutor(executor); }
如果cacheEnabled=true
,會用裝飾器設(shè)計(jì)模式對Executor進(jìn)行裝飾。
第三步:插件代理
緩存裝飾完后,就會執(zhí)行
executor = (Executor) interceptorChain.pluginAll(executor);
這里會對 Executor 植入插件邏輯。
比如:分頁插件中就需要把插件植入的Executor
好了,到此,執(zhí)行器創(chuàng)建的就搞定了。
創(chuàng)建DefaultSqlSession對象
把前面解析配置文件創(chuàng)建的Configuration對象和創(chuàng)建的執(zhí)行器Executor賦給DefaultSqlSession中的屬性。
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { this.configuration = configuration; this.executor = executor; this.dirty = false; this.autoCommit = autoCommit; }
到這里,SqlSession(DefaultSqlSession)對象就創(chuàng)建完畢。
總結(jié)
本文我們講了如何創(chuàng)建SqlSession的幾個(gè)步驟,最后我們獲得一個(gè)DefaultSqlSession對象,里面包含了執(zhí)行器Executor和配置對象Configuration。Executor是SQL的實(shí)際執(zhí)行對象。Configuration里保存著配置文件內(nèi)容。
本文源碼分析的整個(gè)流程如下圖:
到此這篇關(guān)于Mybatis SqlSession詳解的文章就介紹到這了,更多相關(guān)Mybatis SqlSession內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn)
定時(shí)器會執(zhí)行指定的任務(wù),本文主要介紹了Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06解決因jdk版本引起的TypeNotPresentExceptionProxy異常
這篇文章介紹了解決因jdk版本引起的TypeNotPresentExceptionProxy異常的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12PowerJobAutoConfiguration自動(dòng)配置源碼流程解析
這篇文章主要為大家介紹了PowerJobAutoConfiguration自動(dòng)配置源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12