您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mybatis SqlSession怎么創(chuàng)建的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Mybatis SqlSession怎么創(chuàng)建文章都會有所收獲,下面我們一起來看看吧。
先上案例代碼,我們按照這個案例一步一步的搞定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)配置文件的,如果怕迷路,還是建議先看前一篇文章:
Mybatis是如何解析配置文件的?看完終于明白了
繼續(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,這個方法直接調(diào)用本類中的另外一個方法 @Override public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); } //其實(shí)是調(diào)用這個方法 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { //對應(yīng)xml標(biāo)簽<environments> ,這個在配置文件解析的時候就已經(jīng)存放到configuration中了。 final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //創(chuàng)建一個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(); } }
這個方法中的主要內(nèi)容有:
下面我們就來逐個攻破。
事務(wù)工廠類型可以配置為JDBC類型或者M(jìn)ANAGED類型。
JdbcTransactionFactory生產(chǎn)JdbcTransaction。
ManagedTransactionFactory生產(chǎn)ManagedTransaction。
如果配置的JDBC,則會使用Connection對象的commit()、rollback()、close()方法來管理事務(wù)。
如果我們配置的是MANAGED,會把事務(wù)交給容器來管理,比如JBOSS,Weblogic。因為我們是本地跑的程序,如果配置成MANAGED就會不有任何事務(wù)。
但是,如果我們項目中是Spring集成Mybatis,則沒有必要配置事務(wù),因為我們會直接在applicationContext.xml里配置數(shù)據(jù)源和事務(wù)管理器,從而覆蓋Mybatis的配置。
調(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; }
此方法分三個步驟。
第一步:創(chuàng)建執(zhí)行器
Executor的基本類型有三種:
public enum ExecutorType { SIMPLE, REUSE, BATCH }
SIMPLE為默認(rèn)類型。
為什么要讓抽象類BaseExecutor實(shí)現(xiàn)Executor接口,然后讓具體實(shí)現(xiàn)類繼承抽象類呢?
這就是模板方法模式的實(shí)現(xiàn)。
模板方法模式就是定義一個算法骨架,并允許子類為一個或者多個步驟提供實(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è)計模式對Executor進(jìn)行裝飾。
第三步:插件代理
緩存裝飾完后,就會執(zhí)行
executor = (Executor) interceptorChain.pluginAll(executor);
這里會對 Executor 植入插件邏輯。
比如:分頁插件中就需要把插件植入的Executor
好了,到此,執(zhí)行器創(chuàng)建的就搞定了。
把前面解析配置文件創(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; }
關(guān)于“Mybatis SqlSession怎么創(chuàng)建”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Mybatis SqlSession怎么創(chuàng)建”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。