SSM是目前常見的建構Web項目的方案,Mybatis是其中重要的一環,如果能深刻的了解Mybatis的内部原理,對我們會有極大的幫助,接下來一起看看Mybatis的内部設計。
準備
- 搭建Mybatis的基本運作環境,參考 Mybatis入門
- 貼上自己的代碼
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 查詢資料庫内容
sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
System.out.println(user);
// 插入資料庫内容
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
分析
- 擷取Mybatis的配置檔案,内部通過ClassLoader加載檔案流,這一步需要對Classloader有一定的了解,裡面相對簡單,就不多說了
Resources.getResourceAsStream("mybatis.xml");
- 建立SqlSessionFactory, 通過JDK内部的w3c解析配置檔案的内容,封裝到Configration對象中,最後通過Configuration來建立DefaultSqlSessionFactory.
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
- 通過SqlSessionFactory建立SqlSession對象

image.png
- 擷取配置的env參數,在解析配置的時候根據配置生成了TransactionFactory
- TransactionFactory對象,在設定env後,預設是JDBCTransactionFactory
- 通過配置生成預設的executor,executor是很重要的元件,可以看到executor封裝了很多操作資料庫相關的東西
- 生成預設的DefaultSqlSession,内部有executor,TransactionFactory,configuration
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
// 擷取配置的
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
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();
}
}
- SqlSession通過key擷取到與key綁定的sql語句,并且執行,最後擷取到結果。這步内部可以細分
sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
selectOne内部調用的是selectList的函數,selectList函數内部,首先擷取到key對應的MappedStatement,然後通過Executor查詢MapperStatement。
在解析配置的時候建立key與MappedStatement映射關系的
@Override
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
- 不同的executor内部的查詢方法不同,後面我們詳細講解不同的executor。這次先看SimpleExecutor.
- queryStack判斷目前的SQL執行棧,可能會連續執行多條sql語句
- localCache 緩存目前Sqlsession查詢到的對象
- queryFromDatabase 如果沒有緩存從資料庫中進行查詢,重點語句
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
BoundSql boundSql = ms.getBoundSql(parameter);
CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
@SuppressWarnings("unchecked")
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List<E> list;
try {
queryStack++;
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
if (list != null) {
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
// issue #601
deferredLoads.clear();
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
// issue #482
clearLocalCache();
}
}
return list;
}
- query方法中真正的查詢交給子類的doQuery,而SimpleExecutor的doQuery如下
- 建立StatementHandler,語句處理器
- 通過StatementHandler再進行query
- Statement為JDK提供的SQL接口,擷取Statement經曆了StatementHandler.prepare,然後parameterize設定參數。
- query方法利用MySQL内部的query
- 擷取到結果之後,通過resultHandler處理擷取到的結果
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
- StatementHandler内部的調用,結果集處理使用的是DefaultResultSetHandler,内部ResultSet的處理屬于JDBC的知識,想看懂Mybatis,對JDBC也要有一定的了解
- 擷取到結果傳回給我們
User user = sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
回顧
這次大緻看了下Mybatis的基本執行流程,涉及到了幾個關鍵的類
- SqlSessionFactory ,SqlSession
- MappedStatement 封裝我們寫的SQL語句
- StatementHandler 語句處理器
- ResultHandler 傳回的結果處理器
- Executor Mybatis中的語句執行器
建立SqlSessionFactory -> 擷取SqlSession -> 擷取->MappedStatement -> 擷取StatementHandler同時建立Statement -> 執行Statement -> 使用ResultSet處理執行的結果,處理結果根據Mapperr.xml檔案中指定的類型映射最終實作ORM功能
總結
Mybatis的過程相比Spring MVC更直覺一些,不過需要熟悉JDBC的知識,内部還有一些細節,後續繼續研究