mybatis的源碼分析
一、傳統方式源碼分析
分析之前我們來回顧下傳統方式的寫法:
/**
* 傳統方式
*/
public void test1() throws IOException {
// 1. 讀取配置文件,讀成字節輸入流,注意:現在還沒解析
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
// 2. 解析配置文件,封裝Configuration對象 創建DefaultSqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 生產了DefaultSqlsession實例對象 設置了事務不自動提交 完成了executor對象的創建
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4.(1)根據statementid來從Configuration中map集合中獲取到了指定的MappedStatement對象
// (2)將查詢任務委派了executor執行器
List<Object> objects = sqlSession.selectList("namespace.id");
// 5.釋放資源
sqlSession.close();
}
1、初始化
// 1. 讀取配置文件,讀成字節輸入流,注意:現在還沒解析
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
// 2. 解析配置文件,封裝Configuration對象 創建DefaultSqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
初始化操作也就是mybatis框架會讀取到我們本地項目中resources
的mybatis-config.xml
配置文件。
然後開始解析配置文件!!!
廢話不多說,直接跟我走進源碼的世界。
// 1.我們最初調用的build
public SqlSessionFactory build(InputStream inputStream) {
//調用了重載方法
return build(inputStream, null, null);
}
// 2.調用的重載方法
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
// 創建 XMLConfigBuilder, XMLConfigBuilder是專門解析mybatis的配置文件的類
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
// 執行 XML 解析
// 創建 DefaultSqlSessionFactory 對象
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.
}
}
}
MyBatis
在初始化的時候,會將MyBatis
的配置信息全部加載到內存中,使用org.apache.ibatis.session.Configuration
實例來維護。
Configuration
這個類非常重要!!!它是後續解析MyBatis
的核心配置文件以及Mapper.xml
的關鍵。
這樣跟你說吧:Configuration
對象的結構和xml
配置文件的對象幾乎相同。
我們繼續跟上面的parser.parse()
/**
* 解析 XML 成 Configuration 對象。
*
* @return Configuration 對象
*/
public Configuration parse() {
// 若已解析,拋出 BuilderException 異常
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
// 標記已解析
parsed = true;
///parser是XPathParser解析器對象,讀取節點內數據,<configuration>是MyBatis配置文件中的頂層標籤
// 解析 XML configuration 節點
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
/**
* 解析 XML
*
* 具體 MyBatis 有哪些 XML 標籤,參見 《XML 映射配置文件》http://www.mybatis.org/mybatis-3/zh/configuration.html
*
* @param root 根節點
*/
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
// 解析 <properties /> 標籤
propertiesElement(root.evalNode("properties"));
// 解析 <settings /> 標籤
Properties settings = settingsAsProperties(root.evalNode("settings"));
// 加載自定義的 VFS 實現類
loadCustomVfs(settings);
// 解析 <typeAliases /> 標籤
typeAliasesElement(root.evalNode("typeAliases"));
// 解析 <plugins /> 標籤
pluginElement(root.evalNode("plugins"));
// 解析 <objectFactory /> 標籤
objectFactoryElement(root.evalNode("objectFactory"));
// 解析 <objectWrapperFactory /> 標籤
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
// 解析 <reflectorFactory /> 標籤
reflectorFactoryElement(root.evalNode("reflectorFactory"));
// 賦值 <settings /> 到 Configuration 屬性
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
// 解析 <environments /> 標籤
environmentsElement(root.evalNode("environments"));
// 解析 <databaseIdProvider /> 標籤
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
// 解析 <typeHandlers /> 標籤
typeHandlerElement(root.evalNode("typeHandlers"));
// 解析 <mappers /> 標籤
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
這裏介紹下MappedStatement
,這也是一個關鍵的類。爲啥這樣說呢?下面我們來看下它的作用你就知道了:
Configuration
實例中維護了mappedStatements
這個變量,這是一個鍵值對的Map
集合,key
是namespace.id
,value
是MappedStatement
。
MappedStatement
與Mapper
配置文件中的一個select/update/delete/insert
節點對應,mapper的配置文件都封裝到了此對象中,主要用途是描述一條sql
語句。
初始化過程:回顧剛開始介紹的加載配置文件的過程中,會對mybatis-config.xml
中的各個標籤都進行解析,其中mappers
標籤用來引入mapper.xml
文件或者配置mapper
接口的目錄。
<select id="findAll" resultType="com.riemann.pojo.User">
select * from user
</select>
比如上面這個select
標籤會在初始化配置文件時被解析成一個MappedStatement
對象,然後存儲在Configuration
對象的mappedStatements
屬性中,mappedStatements
是一個HashMap
,存儲時key=全限定類名+方法名
,value=對應的MappedStatement對象
。
-
在
Configuration
中對應屬性爲/** * MappedStatement 映射 * * KEY:`${namespace}.${id}` */ protected final Map<String, MappedStatement> mappedStatements = new StrictMap<>("Mapped Statements collection");
-
在
XMLConfigBuilder
中的處理:/** * 解析 XML * * 具體 MyBatis 有哪些 XML 標籤,參見 《XML 映射配置文件》http://www.mybatis.org/mybatis-3/zh/configuration.html * * @param root 根節點 */ private void parseConfiguration(XNode root) { try { ... // 解析 <mappers /> 標籤 mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
我們這裏繼續跟解析mappers
標籤mapperElement(root.evalNode("mappers"));
,其他的解析這裏就不展示了哈。
private void mapperElement(XNode parent) throws Exception {
if (parent != null) {
// 遍歷子節點
for (XNode child : parent.getChildren()) {
// 如果是 package 標籤,則掃描該包
if ("package".equals(child.getName())) {
// 獲得包名
String mapperPackage = child.getStringAttribute("name");
// 添加到 configuration 中
configuration.addMappers(mapperPackage);
// 如果是 mapper 標籤,
} else {
// 獲得 resource、url、class 屬性
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
// 使用相對於類路徑的資源引用
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
// 獲得 resource 的 InputStream 對象
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創建 XMLMapperBuilder 對象
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
// 執行解析
mapperParser.parse();
// 使用完全限定資源定位符(URL)
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
// 獲得 url 的 InputStream 對象
InputStream inputStream = Resources.getUrlAsStream(url);
// 創建 XMLMapperBuilder 對象
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
// 執行解析
mapperParser.parse();
// 使用映射器接口實現類的完全限定類名
} else if (resource == null && url == null && mapperClass != null) {
// 獲得 Mapper 接口
Class<?> mapperInterface = Resources.classForName(mapperClass);
// 添加到 configuration 中
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
}
}
}
}
到此,對xml配置文件的解析就結束了,回到開頭的步驟2.中調用的重載build
方法
/**
* 創建 DefaultSqlSessionFactory 對象
*
* @param config Configuration 對象
* @return DefaultSqlSessionFactory 對象
*/
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config); //構建者設計模式
}
2、執行sql流程
繼續跟着上面的構建者設計模式把Configuration
對象傳到了SqlSessionFactory
。
我們拿到了SqlSessionFactory
以後,調用openSession()
方法。
SqlSession sqlSession = sqlSessionFactory.openSession();
我們先來簡單介紹下SqlSession
:
SqlSession
是一個接口,它有兩個實現類:DefaultSqlSession
(默認) 和 SqlSessionManager
(棄用,不做介紹)
SqlSession
是MyBatis
中用於和數據庫交互的頂層類,通常將它與ThreadLocal
綁定,一個會話使用一個SqlSession
,並且在使用完畢後需要close
。
public class DefaultSqlSession implements SqlSession {
private final Configuration configuration;
private final Executor executor;
...
}
DefaultSqlSession
中兩個最重要的參數,Configuration
與初始化的時候相同,Executor
爲執行器。
繼續分析,初始化完畢後,我們就要執行sql
語句了:
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("com.riemann.mapper.UserMapper.getAllUser");
獲得SqlSession
:
上面瞭解完SqlSession
以及它的默認實現類DefaultSqlSession
後,我們繼續回到上面的sqlSessionFactory.openSession()
分析。如下代碼:
/**
* 默認 SqlSessionFactory 實現類
*
* @author Clinton Begin
*/
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private final Configuration configuration;
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
//6. 進入openSession方法
@Override
public SqlSession openSession() {
//getDefaultExecutorType()傳遞的是SimpleExecutor
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
//7. 進入openSessionFromDataSource。
//ExecutorType 爲Executor的類型,TransactionIsolationLevel爲事務隔離級別,autoCommit是否開啓事務
//openSession的多個重載方法可以指定獲得的SqlSession的Executor類型和事務的處理
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
// 獲得 Environment 對象
final Environment environment = configuration.getEnvironment();
// 創建 Transaction 對象
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
// 創建 Executor 對象
final Executor executor = configuration.newExecutor(tx, execType);
// 創建 DefaultSqlSession 對象
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
// 如果發生異常,則關閉 Transaction 對象
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
中的api
:
public class DefaultSqlSession implements SqlSession {
...
//8.進入selectList方法,多個重載方法
@Override
public <E> List<E> selectList(String statement) {
return this.selectList(statement, null);
}
@Override
public <E> List<E> selectList(String statement, Object parameter) {
return this.selectList(statement, parameter, RowBounds.DEFAULT);
}
@Override
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
// 獲得 MappedStatement 對象
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();
}
}
...
}
3、Executor執行器
Executor也是一個接口,它有三個常用的實現類:
BatchExecutor
:重用語句並執行批量更新ReuseExecutor
:重用預處理語句PreparedStatements
SimpleExecutor
:普通的執行器,默認
繼續跟蹤上面源碼的步驟,到了executor.query()
/**
* Executor 基類,提供骨架方法,從而使子類只要實現指定的幾個抽象方法即可
*/
public abstract class BaseExecutor implements Executor {
...
//此方法在SimpleExecutor的父類BaseExecutor中實現
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
//根據傳入的參數動態獲得SQL語句,最後返回用BoundSql對象表示
BoundSql boundSql = ms.getBoundSql(parameter);
//爲本次查詢創建緩存的Key
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());
// 已經關閉,則拋出 ExecutorException 異常
if (closed) {
throw new ExecutorException("Executor was closed.");
}
// 清空本地緩存,如果 queryStack 爲零,並且要求清空本地緩存。
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List<E> list;
try {
// queryStack + 1
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 - 1
queryStack--;
}
if (queryStack == 0) {
// 執行延遲加載
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
// issue #601
// 清空 deferredLoads
deferredLoads.clear();
// 如果緩存級別是 LocalCacheScope.STATEMENT ,則進行清理
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
// issue #482
clearLocalCache();
}
}
return list;
}
...
}
/**
* 簡單的 Executor 實現類。
*
* 1. 每次開始讀或寫操作,都創建對應的 Statement 對象。
* 2. 執行完成後,關閉該 Statement 對象。
*/
public class SimpleExecutor extends BaseExecutor {
public SimpleExecutor(Configuration configuration, Transaction transaction) {
super(configuration, transaction);
}
@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();
// 傳入參數創建StatementHanlder對象來執行查詢
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
// 創建jdbc中的statement對象
stmt = prepareStatement(handler, ms.getStatementLog());
// 執行 StatementHandler ,進行讀操作
return handler.query(stmt, resultHandler);
} finally {
// 關閉 StatementHandler 對象
closeStatement(stmt);
}
}
// 初始化 StatementHandler 對象
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
// 獲得 Connection 對象
Connection connection = getConnection(statementLog);
// 創建 Statement 或 PrepareStatement 對象
stmt = handler.prepare(connection, transaction.getTimeout());
// 設置 SQL 上的參數,例如 PrepareStatement 對象上的佔位符
handler.parameterize(stmt);
return stmt;
}
}
// 獲得 Connection 對象
protected Connection getConnection(Log statementLog) throws SQLException {
// 獲得 Connection 對象
Connection connection = transaction.getConnection();
// 如果 debug 日誌級別,則創建 ConnectionLogger 對象,進行動態代理
if (statementLog.isDebugEnabled()) {
return ConnectionLogger.newInstance(connection, statementLog, queryStack);
} else {
return connection;
}
}
@Override
public Connection getConnection() throws SQLException {
// 連接爲空,進行創建
if (connection == null) {
openConnection();
}
return connection;
}
/**
* 獲得 Connection 對象
*
* @throws SQLException 獲得失敗
*/
protected void openConnection() throws SQLException {
if (log.isDebugEnabled()) {
log.debug("Opening JDBC Connection");
}
// 獲得連接
connection = dataSource.getConnection();
// 設置隔離級別
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
// 設置 autoCommit 屬性
setDesiredAutoCommit(autoCommit);
}
上述的executor.query()
方法幾經轉折,最後創建一個StatementHandler
對象,然後將必要的參數傳遞給StatementHandler
,使用StatementHandler
來完成對數據庫的查詢,最終返回List
結果集。
從上面的代碼可以看出,Executor
的功能和作用是:
- 據傳遞參數,完成
sql
語句的動態解析,生成BoundSql
對象,供StatementHandler
使用; - 爲查詢
創建緩存
,以提高性能; - 創建
JDBC
的Statement
連接對象,傳遞給StatementHandler
對象,返回List
查詢結果。
4、StatementHandler處理器
StatementHandler
對象主要完成兩個工種:
- 對於
JDBC
的PreparedStatement
類型的對象,創建的過程中,我們使用的時sql語句字符串會包含若干個?
佔位符,我們其後再對佔位符進行設值。StatementHandler
通過parameterize(statement)
方法對Statement
進行設值。 StatementHandler
通過List query(Statement statement, ResultHandler resultHandler)
方法來執行Statement
,將和Statement
對象返回resultSet
封裝成List
。
進入到StatementHandler
的parameterize(statement)
方法的實現:
public class PreparedStatementHandler extends BaseStatementHandler {
...
@Override
public void parameterize(Statement statement) throws SQLException {
//使用ParameterHandler對象來完成對Statement的設值
parameterHandler.setParameters((PreparedStatement) statement);
}
...
}
/**
* 默認 ParameterHandler 實現類
*/
public class DefaultParameterHandler implements ParameterHandler {
...
@Override
public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
// 遍歷 ParameterMapping 數組
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
for (int i = 0; i < parameterMappings.size(); i++) {
// 獲得 ParameterMapping 對象
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
// 獲得值
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
// 獲得 typeHandler、jdbcType 屬性
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
// 設置 ? 佔位符的參數
try {
typeHandler.setParameter(ps, i + 1, value, jdbcType);
} catch (TypeException | SQLException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
}
}
}
}
}
...
}
從上述代碼可以看到StatementHandler
的parameterize(statement)
方法調用了ParameterHandler
的setParameters(preparedStatement)
方法。
該方法負責根據我們傳入的參數,對statement
對象的?
佔位符進行賦值。
進入到StatementHandler
的List query(Statement statement, ResultHandler resultHandler)
public class PreparedStatementHandler extends BaseStatementHandler {
...
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
// 1.調用preparedStatement.execute()方法,然後將resultSet交給ResultSetHandler處理
PreparedStatement ps = (PreparedStatement) statement;
// 2.執行查詢
ps.execute();
// 3.使用ResultSetHandler來處理ResultSet
return resultSetHandler.handleResultSets(ps);
}
...
}
從上述代碼可以看出:StatementHandler
的List query(Statement statement, ResultHandler resultHandler)
方法實現,是調用了ResultSetHandler
的handleResultSets(preparedStatement)
方法。該方法會將Statement
語句執行後生成resultSet
結果集轉換成List
結果集。
public class DefaultResultSetHandler implements ResultSetHandler {
...
@Override
public List<Object> handleResultSets(Statement stmt) throws SQLException {
ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
// 多 ResultSet 的結果集合,每個 ResultSet 對應一個 Object 對象。而實際上,每個 Object 是 List<Object> 對象。
// 在不考慮存儲過程的多 ResultSet 的情況,普通的查詢,實際就一個 ResultSet ,也就是說,multipleResults 最多就一個元素。
final List<Object> multipleResults = new ArrayList<>();
int resultSetCount = 0;
// 獲得首個 ResultSet 對象,並封裝成 ResultSetWrapper 對象
ResultSetWrapper rsw = getFirstResultSet(stmt);
// 獲得 ResultMap 數組
// 在不考慮存儲過程的多 ResultSet 的情況,普通的查詢,實際就一個 ResultSet ,也就是說,resultMaps 就一個元素。
List<ResultMap> resultMaps = mappedStatement.getResultMaps();
int resultMapCount = resultMaps.size();
validateResultMapsCount(rsw, resultMapCount); // 校驗
while (rsw != null && resultMapCount > resultSetCount) {
// 獲得 ResultMap 對象
ResultMap resultMap = resultMaps.get(resultSetCount);
// 處理 ResultSet ,將結果添加到 multipleResults 中
handleResultSet(rsw, resultMap, multipleResults, null);
// 獲得下一個 ResultSet 對象,並封裝成 ResultSetWrapper 對象
rsw = getNextResultSet(stmt);
// 清理
cleanUpAfterHandlingResultSet();
// resultSetCount ++
resultSetCount++;
}
// 因爲 `mappedStatement.resultSets` 只在存儲過程中使用,本系列暫時不考慮,忽略即可
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++;
}
}
// 如果是 multipleResults 單元素,則取首元素返回
return collapseSingleResultList(multipleResults);
}
...
}
OK,至此傳統方式源碼分析到此結束,我們繼續來分析Mapper代理方式的源碼。
二、Mapper代理方式源碼分析
分析之前,我們來回顧下寫法:
/**
* mapper代理方式
*/
public void test2() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = factory.openSession();
// 使用JDK動態代理對mapper接口產生代理對象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//代理對象調用接口中的任意方法,執行的都是動態代理中的invoke方法
List<User> allUser = mapper.findAllUser();
}
思考一個問題:通常Mapper接口我們都沒有實現的方法卻可以使用,是爲什麼呢?答案很簡單:動態代理。
開始之前介紹一下MyBatis
初始化時對接口的處理:MapperRegistry
是Configuration
中的一個屬性,它內部維護了一個HashMap
,用於存放mapper
接口的工廠類,每個接口對應一個工廠。
/**
* MapperRegistry 對象
*/
protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
mappers中可以配置接口的包路徑,或者某個接口的具體類。
<mappers>
<!--普通寫法-->
<!--<mapper resource="OrderMapper.xml" />-->
<!--註解寫法-->
<!--<mapper class="com.riemann.mapper.UserMapper" />-->
<!--上面那個方法如果mapper很多的話,要些很多個,所以這裏推薦包掃描-->
<!--這裏要注意!!!要在resources裏建與上面同包同名 這種形式建立目錄:com/riemann/mapper-->
<package name="com.riemann.mapper"/>
</mappers>
當解析mappers標籤時,它會判斷解析到的是mapper
配置文件時,會再將對應配置文件中的增刪改查標籤一一封裝成MappedStatement
對象,存入mappedStatements
中。當判斷解析到接口時,會建此接口對應的MappedProxyFactory
對象,存入HashMap
中,key=接口的字節碼對象,value=此接口對應的MappedProxyFactory對象
。
1、getMapper分析
// DefaultSqlSession的getMapper
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
// Configuration的getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
// MapperRegistry的getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
// 獲得 MapperProxyFactory 對象
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
// 不存在,則拋出 BindingException 異常
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
/// 通過動態代理工廠生成實例。
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
}
//MapperProxyFactory類中的newInstance方法
public T newInstance(SqlSession sqlSession) {
// 創建了JDK動態代理的invocationHandler接口的實現類mapperProxy
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
// 調用了重載方法
return newInstance(mapperProxy);
}
public class MapperProxy<T> implements InvocationHandler, Serializable {
/**
* SqlSession 對象
*/
private final SqlSession sqlSession;
/**
* Mapper 接口
*/
private final Class<T> mapperInterface;
/**
* 方法與 MapperMethod 的映射
*
* 從 {@link MapperProxyFactory#methodCache} 傳遞過來
*/
private final Map<Method, MapperMethod> methodCache;
// 構造,傳入了SqlSession,說明每個session中的代理對象的不同的!
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// 如果是 Object 定義的方法,直接調用
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
// 獲得 MapperMethod 對象
final MapperMethod mapperMethod = cachedMapperMethod(method);
// 重點在這:MapperMethod最終調用了執行的方法
return mapperMethod.execute(sqlSession, args);
}
...
}
2、invoke()分析
在動態代理返回了示例後,我們就可以直接調用mapper類中的方法了,但代理對象調用方法,執行是在MapperProxy
的invoke
方法中。
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// 如果是 Object 定義的方法,直接調用
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
// 獲得 MapperMethod 對象
final MapperMethod mapperMethod = cachedMapperMethod(method);
// 重點在這:MapperMethod最終調用了執行的方法
return mapperMethod.execute(sqlSession, args);
}
進入execute方法
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
//判斷mapper中的方法類型,最終調用的還是SqlSession中的方法
switch (command.getType()) {
case INSERT: {
// 轉換參數
Object param = method.convertArgsToSqlCommandParam(args);
// 執行 INSERT 操作
// 轉換 rowCount
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
// 轉換參數
Object param = method.convertArgsToSqlCommandParam(args);
// 轉換 rowCount
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
// 轉換參數
Object param = method.convertArgsToSqlCommandParam(args);
// 轉換 rowCount
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
// 無返回,並且有 ResultHandler 方法參數,則將查詢的結果,提交給 ResultHandler 進行處理
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
// 執行查詢,返回列表
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
// 執行查詢,返回 Map
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
// 執行查詢,返回 Cursor
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
// 執行查詢,返回單個對象
} else {
// 轉換參數
Object param = method.convertArgsToSqlCommandParam(args);
// 查詢單條
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());
}
// 返回結果爲 null ,並且返回類型爲基本類型,則拋出 BindingException 異常
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;
}
OK,至此Mapper代理方式源碼分析到此結。
三、MyBatis源碼中涉及到的設計模式
mybatis
源碼中至少用到了以下的設計模式的使用:
模式 | mybatis體現 |
---|---|
Builder模式 | 例如SqlSessionFactoryBuilder、Environment |
工廠方法模式 | 例如SqlSessionFactory、TransactionFactory、LogFactory |
單例模式 | 例如ErrorContext、LogFactory |
代理模式 | MyBatis實現的核心,例如MapperProxy、ConnectionLogger,用的jdk的動態代理還有executor.loader包使用了cglib或者javassist達到延遲加載的效果。 |
組合模式 | 例如SqlNode和各個子類ChooseSqlNode |
模板方法模式 | 例如BaseExecutor、SimpleExecutor |
適配器模式 | 例如Log的MyBatis接口和它對jdbc、log4j等各種日誌框架的適配實現 |
裝飾者模式 | 例如Cache包中的cache.decorators子包中等各個裝飾者的實現 |
迭代器模式 | 例如PropertyTokenizer |