該系列檔案是本人在學習 Mybatis 的原始碼程序中總結下來的,可能對讀者不太友好,請結合我的原始碼注釋(Mybatis原始碼分析 GitHub 地址、Mybatis-Spring 原始碼分析 GitHub 地址、Spring-Boot-Starter 原始碼分析 GitHub 地址)進行閱讀
MyBatis 版本:3.5.2
MyBatis-Spring 版本:2.0.3
MyBatis-Spring-Boot-Starter 版本:2.1.4
SqlSession會話與SQL執行入口
在前面一系列的檔案中,已經詳細的介紹了 MyBatis 的初始化和執行 SQL 的程序,在執行 SQL 的程序中,還存在著一些疑問,例如其中一直提到的 SqlSession 會話在 MyBatis 中是如何被創建的?如何呼叫 Executor 執行器執行 SQL 的?
那么接下來就關于上面兩個的兩個問題,一起來探討一下 MyBatis 中 SqlSession 會話的相關內容
先回顧一下《基礎支持層》的Binding模塊,每個Mapper Interface會有對應的MapperProxyFactory動態代理物件工廠,用于創建MapperProxy動態代理物件,Mapper 介面中的每個方法都有對應的MapperMethod物件,該物件會通過 SqlSession 會話執行資料操作
再來看到這張MyBatis整體圖:
在單獨使用 MyBatis 進行資料庫操作時,需要通過SqlSessionFactoryBuilder構建一個SessionFactory物件,然后通過它創建一個SqlSession會話進行資料庫操作
我們通常都會先呼叫SqlSession會話的getMapper(Class<T> mapper)方法,為 Mapper 介面生成一個“實作類”(JDK動態代理物件),然后就可以通過它進行資料庫操作
示例
// <1> 構建 SqlSessionFactory 物件
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/autoconstructor/mybatis-config.xml");
// <2> 默認 DefaultSqlSessionFactory 物件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// <3> 獲得 SqlSession 物件,默認 DefaultSqlSession 物件
SqlSession sqlSession = sqlSessionFactory.openSession();
// <4> 獲得 Mapper 物件
final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
// <5> 執行查詢
final Object subject = mapper.getSubject(1);
SqlSessionFactoryBuilder
org.apache.ibatis.session.SqlSessionFactoryBuilder:構建SqlSessionFactory工廠類,里面定義了許多build多載方法,主要分為處理Reader和InputStream兩種檔案資源物件,代碼如下:
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(Reader reader) {
return build(reader, null, null);
}
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
public SqlSessionFactory build(Reader reader, Properties properties) {
return build(reader, null, properties);
}
/**
* 構造 SqlSessionFactory 物件
*
* @param reader Reader 物件
* @param environment 環境
* @param properties Properties 變數
* @return SqlSessionFactory 物件
*/
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
/*
* <1> 創建 XMLConfigBuilder 物件
* 會生成一個 XPathParser,包含 Document 物件
* 會創建一個 Configuration 全域配置物件
*/
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
/*
* <2> 決議 XML 檔案并配置到 Configuration 全域配置物件中
* <3> 創建 DefaultSqlSessionFactory 物件
*/
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment) {
return build(inputStream, environment, null);
}
public SqlSessionFactory build(InputStream inputStream, Properties properties) {
return build(inputStream, null, properties);
}
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);
}
}
在《MyBatis初始化(一)之加載mybatis-config.xml》中已經分析了,這里就不再贅述,就是根據檔案資源創建Configuration全域配置物件,然后構建一個DefaultSqlSessionFactory物件
DefaultSqlSessionFactory
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory:實作 SqlSessionFactory 介面,默認的 SqlSessionFactory 實作類
openSession方法
openSession方法,創建一個DefaultSqlSession物件,如下:
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private final Configuration configuration;
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
@Override
public SqlSession openSession(boolean autoCommit) {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
}
@Override
public SqlSession openSession(ExecutorType execType) {
return openSessionFromDataSource(execType, null, false);
}
@Override
public SqlSession openSession(TransactionIsolationLevel level) {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);
}
@Override
public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
return openSessionFromDataSource(execType, level, false);
}
@Override
public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
return openSessionFromDataSource(execType, null, autoCommit);
}
@Override
public SqlSession openSession(Connection connection) {
return openSessionFromConnection(configuration.getDefaultExecutorType(), connection);
}
@Override
public SqlSession openSession(ExecutorType execType, Connection connection) {
return openSessionFromConnection(execType, connection);
}
}
openSession有很多多載的方法,主要是提供以下幾種入參的支持:
| 型別 | 引數名稱 | 默認值 | 描述 |
|---|---|---|---|
| boolean | autoCommit | false | 事務是否自動提交 |
| ExecutorType | execType | ExecutorType.SIMPLE | Executor執行器型別 |
| TransactionIsolationLevel | level | 無 | 事務隔離級別 |
| java.sql.Connection | connection | 無 | 資料庫連接 |
內部直接呼叫openSessionFromDataSource私有方法,內部也需要呼叫openSessionFromConnection私有方法,如果存在connection入參,內部則直接呼叫openSessionFromConnection私有方法
openSessionFromDataSource方法
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法,用于創建一個DefaultSqlSession物件,方法如下:
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();
}
}
- 獲得
Environment當前環境物件 - 通過
getTransactionFactoryFromEnvironment方法,從 Environment 環境物件中TransactionFactory物件,用于創建一個Transaction事務 - 然后再創建一個
Executor執行器物件 - 根據全域配置物件、執行器和事務是否自動提交創建一個
DefaultSqlSession物件
openSessionFromConnection方法
openSessionFromConnection(ExecutorType execType, Connection connection)方法,用于創建一個DefaultSqlSession物件
和上面的方法邏輯相同,只不過它的入參直接傳入了一個Connection資料庫連接,方法如下:
private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
try {
// 獲得是否可以自動提交
boolean autoCommit;
try {
autoCommit = connection.getAutoCommit();
} catch (SQLException e) {
// Failover to true, as most poor drivers
// or databases won't support transactions
autoCommit = true;
}
// 獲得 Environment 物件
final Environment environment = configuration.getEnvironment();
// 創建 Transaction 物件
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
final Transaction tx = transactionFactory.newTransaction(connection);
// 創建 Executor 物件
final Executor executor = configuration.newExecutor(tx, execType);
// 創建 DefaultSqlSession 物件
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
和上面的方法不同的是,創建Transaction事務物件時,傳入的引數直接是Connection資料庫連接物件
getTransactionFactoryFromEnvironment方法
getTransactionFactoryFromEnvironment(Environment environment)方法,用于創建一個TransactionFactory物件,在創建 SqlSessionFactory 時,就可以通過設定 Environment 的 DataSource 資料源和 TransactionFactory 事務工廠來集成第三方資料源和事務管理器,代碼如下:
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
// 情況一,創建 ManagedTransactionFactory 物件
if (environment == null || environment.getTransactionFactory() == null) {
return new ManagedTransactionFactory();
}
// 情況二,使用 `environment` 中的
return environment.getTransactionFactory();
}
closeTransaction方法
closeTransaction(Transaction tx)方法,關閉事務,方法如下:
private void closeTransaction(Transaction tx) {
if (tx != null) {
try {
tx.close();
} catch (SQLException ignore) {
// Intentionally ignore. Prefer previous error.
}
}
}
DefaultSqlSession
org.apache.ibatis.session.defaults.DefaultSqlSession:實作 SqlSession 介面,默認的 SqlSession 實作類,呼叫 Executor 執行器,執行資料庫操作
構造方法
public class DefaultSqlSession implements SqlSession {
/**
* 全域配置
*/
private final Configuration configuration;
/**
* 執行器物件
*/
private final Executor executor;
/**
* 是否自動提交事務
*/
private final boolean autoCommit;
/**
* 是否發生資料變更
*/
private boolean dirty;
/**
* Cursor 陣列
*/
private List<Cursor<?>> cursorList;
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}
}
select方法
執行資料庫查詢操作,提供了許多多載方法
@Override
public void select(String statement, Object parameter, ResultHandler handler) {
select(statement, parameter, RowBounds.DEFAULT, handler);
}
@Override
public void select(String statement, ResultHandler handler) {
select(statement, null, RowBounds.DEFAULT, handler);
}
@Override
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
executor.query(ms, wrapCollection(parameter), rowBounds, handler);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
@Override
public <T> T selectOne(String statement) {
return this.selectOne(statement, null);
}
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException(
"Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
@Override
public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
return this.selectMap(statement, null, mapKey, RowBounds.DEFAULT);
}
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
return this.selectMap(statement, parameter, mapKey, RowBounds.DEFAULT);
}
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
// <1> 執行查詢
final List<? extends V> list = selectList(statement, parameter, rowBounds);
// <2> 創建 DefaultMapResultHandler 物件
final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<>(mapKey,
configuration.getObjectFactory(), configuration.getObjectWrapperFactory(),
configuration.getReflectorFactory());
// <3> 創建 DefaultResultContext 物件
final DefaultResultContext<V> context = new DefaultResultContext<>();
// <4> 遍歷查詢結果
for (V o : list) {
// 設定 DefaultResultContext 中
context.nextResultObject(o);
// 使用 DefaultMapResultHandler 處理結果的當前元素
mapResultHandler.handleResult(context);
}
// <5> 回傳結果
return mapResultHandler.getMappedResults();
}
@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 {
// <1> 獲得 MappedStatement 物件
MappedStatement ms = configuration.getMappedStatement(statement);
// <2> 執行查詢
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();
}
}
上面有很多的資料庫查詢方法,主要分為以下幾種:
select:執行資料庫查詢操作,通過入參中的ResultHandler處理結果集,無回傳結果selectList:執行資料庫查詢操作,回傳List集合selectOne:呼叫selectList方法,執行資料庫查詢操作,最多只能回傳一條資料selectMap:呼叫selectList方法,執行資料庫查詢操作,通過DefaultMapResultHandler進行處理,將回傳結果轉換成Map集合
可以看到通過Executor執行器的query方法執行查詢操作,可以回顧《SQL執行程序(一)之Executor》中的內容
這里會先呼叫wrapCollection方法對入參進行包裝(如果是集合型別)
update方法
執行資料庫更新操作
@Override
public int insert(String statement) {
return insert(statement, null);
}
@Override
public int insert(String statement, Object parameter) {
return update(statement, parameter);
}
@Override
public int delete(String statement) {
return update(statement, null);
}
@Override
public int delete(String statement, Object parameter) {
return update(statement, parameter);
}
@Override
public int update(String statement) {
return update(statement, null);
}
@Override
public int update(String statement, Object parameter) {
try {
// <1> 標記 dirty ,表示執行過寫操作
dirty = true;
// <2> 獲得 MappedStatement 物件
MappedStatement ms = configuration.getMappedStatement(statement);
// <3> 執行更新操作
return executor.update(ms, wrapCollection(parameter));
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
insert和delete方法最終都是呼叫update方法,通過Executor執行器的update方法執行資料庫更新操作
這里會先呼叫wrapCollection方法對入參進行包裝(如果是集合型別)
wrapCollection方法
wrapCollection(final Object object)方法,將集合型別的引數包裝成StrictMap物件,方法如下:
public static class StrictMap<V> extends HashMap<String, V> {
private static final long serialVersionUID = -5741767162221585340L;
@Override
public V get(Object key) {
if (!super.containsKey(key)) {
throw new BindingException(
"Parameter '" + key + "' not found. Available parameters are " + this.keySet());
}
return super.get(key);
}
}
private Object wrapCollection(final Object object) {
if (object instanceof Collection) {
// 如果是集合,則添加到 collection 中
StrictMap<Object> map = new StrictMap<>();
map.put("collection", object);
// 如果是 List ,則添加到 list 中
if (object instanceof List) {
map.put("list", object);
}
return map;
} else if (object != null && object.getClass().isArray()) {
// 如果是 Array ,則添加到 array 中
StrictMap<Object> map = new StrictMap<>();
map.put("array", object);
return map;
}
return object;
}
getMapper方法
getMapper(Class<T> type)方法,獲取Mapper介面的代理物件
@Override
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
通過Configuration全域配置物件獲取一個動態代理物件
實際通過MapperRegistry注冊表獲取到該Mapper介面對應的MapperProxyFactory動態代理工廠,然后創建一個MapperProxy動態代理的實體物件
其他方法
flushStatements():提交批處理commit():提交事務rollback():回滾事務close():關閉當前會話getConnection():獲取當前事務的資料庫連接clearCache():清理一級快取
MapperMethod
org.apache.ibatis.binding.MapperMethod:Mapper介面中定義的方法對應的Mapper方法,通過它來執行SQL
先來看看執行一個SQL的完整流程圖:
在《基礎支持層》的Binding模塊已經講過了相關的內容,例如看到前面示例的第4步,通過DefaultSqlSession的getMapper方法會執行以下操作:
- 創建Mapper介面對應的
MapperProxyFactory動態代理物件工廠 - 通過這個工廠的
newInstance方法會創建一個MapperProxy介面代理類,然后回傳該Mapper介面的動態代理物件
當你呼叫這個介面的某個方法時,會進入這個MapperProxy代理類,我們來看到它的invoke方法是如何實作的,方法如下:
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// <1> 如果是 Object 定義的方法,直接呼叫
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (method.isDefault()) { // 是否有 default 修飾的方法
// 針對Java7以上版本對動態型別語言的支持
if (privateLookupInMethod == null) {
return invokeDefaultMethodJava8(proxy, method, args);
} else {
return invokeDefaultMethodJava9(proxy, method, args);
}
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
// <2.1> 獲得 MapperMethod 物件
final MapperMethod mapperMethod = cachedMapperMethod(method);
// <2.2> 執行 MapperMethod 方法
return mapperMethod.execute(sqlSession, args);
}
首先獲取到方法對應的MapperMethod物件,然后通過該物件去執行資料庫的操作
execute方法
public Object execute(SqlSession sqlSession, Object[] args) {
// 根據 SqlCommand 的 Type 判斷應該如何執行 SQL 陳述句
Object result;
switch (command.getType()) {
case INSERT: {
// <1> 獲取引數值與引數名的映射
Object param = method.convertArgsToSqlCommandParam(args);
// <2> 然后通過 SqlSession 進行資料庫更新操作,并將受影響行數轉換為結果
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) { // 無回傳,且入參中有 ResultHandler 結果處理器
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
// 執行查詢,回傳串列
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
// 執行查詢,回傳 Map
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
// 執行查詢,回傳 Cursor
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;
}
根據 SqlCommand 的 Type 判斷應該如何執行 SQL 陳述句,型別分為INSERT、UPDATE、DELETE、SELECT和FLUSH(進行批處理)五種
前三種都屬于資料庫的更新操作,呼叫的是DefaultSqlSession的update方法,通過rowCountResult(int rowCount)將受影響行數轉換為回傳物件
查詢陳述句的話就分為以下幾種情況:
- 無回傳,且入參中有
ResultHandler結果處理器,則呼叫executeWithResultHandler方法,執行查詢,回傳結果設定為null - 回傳物件為多條資料,則呼叫
executeForMany方法,執行查詢,回傳串列 - 回傳物件為Map型別,則呼叫
executeForMap方法,執行查詢,回傳Map - 回傳物件為Cursor型別,則呼叫
executeForCursor方法,執行查詢,回傳Cursor - 回傳物件為單個物件,則呼叫
DefaultSqlSession的selectOne方法,執行查詢,回傳單個物件
上面執行資料庫操作前會先呼叫convertArgsToSqlCommandParam方法,獲取引數值與引數名的映射
convertArgsToSqlCommandParam方法
convertArgsToSqlCommandParam(Object[] args)方法,根據入參回傳引數名稱與入參的映射,方法如下:
public Object convertArgsToSqlCommandParam(Object[] args) {
return paramNameResolver.getNamedParams(args);
}
在《基礎支持層》的反射模塊的ParamNameResolver小節已經分析過該方法,可以跳過去看看
rowCountResult方法
rowCountResult(int rowCount)方法,將受影響行數轉換為結果,方法如下:
private Object rowCountResult(int rowCount) {
final Object result;
if (method.returnsVoid()) {
result = null;
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
result = rowCount;
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
result = (long) rowCount;
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
result = rowCount > 0;
} else {
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: "
+ method.getReturnType());
}
return result;
}
executeWithResultHandler方法
executeWithResultHandler(SqlSession sqlSession, Object[] args)方法,通過入參中定義的ResultHandler結果處理器執行查詢,方法如下:
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
// <1> 獲得 MappedStatement 物件
MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
/*
* <2> 配置校驗
* 因為入參定義了 ResultHandler 結果處理器,所以如果不是存盤程序,且沒有配置回傳結果的 Java Type,則會拋出例外
*/
if (!StatementType.CALLABLE.equals(ms.getStatementType())
&& void.class.equals(ms.getResultMaps().get(0).getType())) {
throw new BindingException(
"method " + command.getName() + " needs either a @ResultMap annotation, a @ResultType annotation,"
+ " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
}
// <3> 獲取引數名稱與入參的映射
Object param = method.convertArgsToSqlCommandParam(args);
// <4> 執行資料庫查詢操作
if (method.hasRowBounds()) { // <4.1> 入參定義了 RowBounds 分頁物件
// <4.1.1> 獲取入參定義了 RowBounds 分頁物件
RowBounds rowBounds = method.extractRowBounds(args);
// <4.1.2> 執行查詢
sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
} else {
// <4.2> 執行查詢
sqlSession.select(command.getName(), param, method.extractResultHandler(args));
}
}
- 獲得
MappedStatement物件 - 配置校驗,因為入參定義了
ResultHandler結果處理器,所以如果不是存盤程序,且沒有配置回傳結果的 Java Type,則會拋出例外 - 獲取引數名稱與入參的映射
- 執行資料庫查詢操作
- 入參定義了
RowBounds分頁物件,則獲取該物件,然后執行查詢,傳入分析物件 - 沒有定義則執行執行查詢
- 入參定義了
executeForMany方法
executeForMany(SqlSession sqlSession, Object[] args)方法,執行查詢,回傳串列,方法如下:
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
List<E> result;
// 獲取引數名稱與入參的映射
Object param = method.convertArgsToSqlCommandParam(args);
// 執行資料庫查詢操作
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
// 執行查詢,回傳 List 集合
result = sqlSession.selectList(command.getName(), param, rowBounds);
} else {
// 執行查詢,回傳 List 集合
result = sqlSession.selectList(command.getName(), param);
}
// issue #510 Collections & arrays support
// 封裝 Array 或 Collection 結果
if (!method.getReturnType().isAssignableFrom(result.getClass())) { // 如果不是 List 集合型別
if (method.getReturnType().isArray()) {
// 將 List 轉換成 Array 陣列型別的結果
return convertToArray(result);
} else {
// 轉換成其他 Collection 集合型別的結果
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
}
// 直接回傳 List 集合型別的結果
return result;
}
- 獲取引數名稱與入參的映射
- 執行資料庫查詢操作,獲取到 List 集合結果
- 根據回傳結果的型別進行轉換
- 如果是 Array 陣列型別,則將 List 轉換成 Array 陣列型別的結果
- 如果是其他集合型別,則將 List 轉換成其他 Collection 集合型別的結果
- 否則直接回傳 List 集合型別的結果
executeForMap方法
executeForMap(SqlSession sqlSession, Object[] args)方法,執行查詢,回傳Map,方法如下:
private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Map<K, V> result;
// 獲取引數名稱與入參的映射
Object param = method.convertArgsToSqlCommandParam(args);
// 執行 SELECT 操作
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
// 執行查詢,回傳 Map 集合
result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
// 執行查詢,回傳 Map 集合
result = sqlSession.selectMap(command.getName(), param, method.getMapKey());
}
return result;
}
- 獲取引數名稱與入參的映射
- 執行查詢,回傳 Map 集合
總結
本分分析了 SqlSession 會話在 MyBatis 中是如何被創建,如何獲取到 Mapper 介面的動態代理物件,通過該動態代理物件是如何執行 SQL 的
-
SqlSessionFactoryBuilder構造器提供build方法,根據mybatis-config.xml組態檔對 MyBatis 進行初始化,生成Configuration物件,用于構建一個DefaultSqlSessionFactory物件 -
通過
1生成的 SqlSession 工廠物件,我們可以構建一個DefaultSqlSession會話物件,通過這個會話物件的getMapper(Class<T> mapper)方法,可以為 Mapper 介面創建一個動態代理物件(JDK動態代理),其動態代理類為MapperProxy物件 -
呼叫 Mapper 介面的某個方法時,會進入相應的
MapperProxy代理類,根據方法對應的MapperMethod物件,執行資料庫操作 -
執行資料庫相關操作,呼叫的就是上面的
DefaultSqlSession會話物件的相關方法,該會話內部則會通過Executor執行器去執行資料庫的相關操作,并回傳執行結果
好了,對于 MyBatis 整體上所有的內容已經全部分析完了,相信大家對 MyBatis 有了一個全面認識,其中肯定有不對或者迷惑的地方,歡迎指正!!!感謝大家的閱讀!!!??????
參考文章:芋道原始碼《精盡 MyBatis 原始碼分析》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/227878.html
標籤:Java
