前面的章節主要講mybatis如何解析配置文件,這些都是一次性的過程。從本章開始講解動態的過程,它們跟應用程序對mybatis的調用密切相關。本章先從sqlsession開始。
創建
正如其名,Sqlsession對應着一次數據庫會話。由於數據庫回話不是永久的,因此Sqlsession的生命週期也不應該是永久的,相反,在你每次訪問數據庫時都需要創建它(當然並不是說在Sqlsession裏只能執行一次sql,你可以執行多次,當一旦關閉了Sqlsession就需要重新創建它)。創建Sqlsession的地方只有一個,那就是SqlsessionFactory的openSession方法:
- public SqlSessionopenSession() {
- returnopenSessionFromDataSource(configuration.getDefaultExecutorType(),null, false);
- }
public SqlSessionopenSession() {
returnopenSessionFromDataSource(configuration.getDefaultExecutorType(),null, false);
}
我們可以看到實際創建SqlSession的地方是openSessionFromDataSource,如下:
- private SqlSessionopenSessionFromDataSource(ExecutorType execType, TransactionIsolationLevellevel, boolean autoCommit) {
- Connectionconnection = null;
- try {
- finalEnvironment environment = configuration.getEnvironment();
- final DataSourcedataSource = getDataSourceFromEnvironment(environment);
- TransactionFactory transactionFactory =getTransactionFactoryFromEnvironment(environment);
- connection = dataSource.getConnection();
- if (level != null) {
- connection.setTransactionIsolation(level.getLevel());
- }
- connection = wrapConnection(connection);
- Transaction tx = transactionFactory.newTransaction(connection,autoCommit);
- Executorexecutor = configuration.newExecutor(tx, execType);
- returnnewDefaultSqlSession(configuration, executor, autoCommit);
- } catch (Exceptione) {
- closeConnection(connection);
- throwExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
- } finally {
- ErrorContext.instance().reset();
- }
- }
private SqlSessionopenSessionFromDataSource(ExecutorType execType, TransactionIsolationLevellevel, boolean autoCommit) {
Connectionconnection = null;
try {
finalEnvironment environment = configuration.getEnvironment();
final DataSourcedataSource = getDataSourceFromEnvironment(environment);
TransactionFactory transactionFactory =getTransactionFactoryFromEnvironment(environment);
connection = dataSource.getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
connection = wrapConnection(connection);
Transaction tx = transactionFactory.newTransaction(connection,autoCommit);
Executorexecutor = configuration.newExecutor(tx, execType);
returnnewDefaultSqlSession(configuration, executor, autoCommit);
} catch (Exceptione) {
closeConnection(connection);
throwExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
可以看出,創建sqlsession經過了以下幾個主要步驟:
1) 從配置中獲取Environment;
2) 從Environment中取得DataSource;
3) 從Environment中取得TransactionFactory;
4) 從DataSource裏獲取數據庫連接對象Connection;
5) 在取得的數據庫連接上創建事務對象Transaction;
6) 創建Executor對象(該對象非常重要,事實上sqlsession的所有操作都是通過它完成的);
7) 創建sqlsession對象。
Executor的創建
Executor與Sqlsession的關係就像市長與書記,Sqlsession只是個門面,真正幹事的是Executor,Sqlsession對數據庫的操作都是通過Executor來完成的。與Sqlsession一樣,Executor也是動態創建的:
- public ExecutornewExecutor(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);
- } elseif(ExecutorType.REUSE == executorType) {
- executor = new ReuseExecutor(this,transaction);
- } else {
- executor = newSimpleExecutor(this, transaction);
- }
- if (cacheEnabled) {
- executor = new CachingExecutor(executor);
- }
- executor =(Executor) interceptorChain.pluginAll(executor);
- return executor;
- }
public ExecutornewExecutor(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);
} elseif(ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this,transaction);
} else {
executor = newSimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor =(Executor) interceptorChain.pluginAll(executor);
return executor;
}
可以看出,如果不開啓cache的話,創建的Executor只是3中基礎類型之一,BatchExecutor專門用於執行批量sql操作,ReuseExecutor會重用statement執行sql操作,SimpleExecutor只是簡單執行sql沒有什麼特別的。開啓cache的話(默認是開啓的並且沒有任何理由去關閉它),就會創建CachingExecutor,它以前面創建的Executor作爲唯一參數。CachingExecutor在查詢數據庫前先查找緩存,若沒找到的話調用delegate(就是構造時傳入的Executor對象)從數據庫查詢,並將查詢結果存入緩存中。
Executor對象是可以被插件攔截的,如果定義了針對Executor類型的插件,最終生成的Executor對象是被各個插件插入後的代理對象(關於插件會有後續章節專門介紹,敬請期待)。
Mapper
Mybatis官方手冊建議通過mapper對象訪問mybatis,因爲使用mapper看起來更優雅,就像下面這樣:
- session = sqlSessionFactory.openSession();
- UserDao userDao= session.getMapper(UserDao.class);
- UserDto user =new UserDto();
- user.setUsername("iMbatis");
- user.setPassword("iMbatis");
- userDao.insertUser(user);
session = sqlSessionFactory.openSession();
UserDao userDao= session.getMapper(UserDao.class);
UserDto user =new UserDto();
user.setUsername("iMbatis");
user.setPassword("iMbatis");
userDao.insertUser(user);
那麼這個mapper到底是什麼呢,它是如何創建的呢,它又是怎麼與sqlsession等關聯起來的呢?下面爲你一一解答。
創建
表面上看mapper是在sqlsession裏創建的,但實際創建它的地方是MapperRegistry:
- public <T>T getMapper(Class<T> type, SqlSession sqlSession) {
- if (!knownMappers.contains(type))
- thrownewBindingException("Type " + type + " isnot known to the MapperRegistry.");
- try {
- returnMapperProxy.newMapperProxy(type, sqlSession);
- } catch (Exceptione) {
- thrownewBindingException("Error getting mapper instance. Cause: " + e, e);
- }
- }
public <T>T getMapper(Class<T> type, SqlSession sqlSession) {
if (!knownMappers.contains(type))
thrownewBindingException("Type " + type + " isnot known to the MapperRegistry.");
try {
returnMapperProxy.newMapperProxy(type, sqlSession);
} catch (Exceptione) {
thrownewBindingException("Error getting mapper instance. Cause: " + e, e);
}
}
可以看到,mapper是一個代理對象,它實現的接口就是傳入的type,這就是爲什麼mapper對象可以通過接口直接訪問。同時還可以看到,創建mapper代理對象時傳入了sqlsession對象,這樣就把sqlsession也關聯起來了。我們進一步看看MapperProxy.newMapperProxy(type,sqlSession);背後發生了什麼事情:
- publicstatic <T>T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
- ClassLoaderclassLoader = mapperInterface.getClassLoader();
- Class<?>[] interfaces = new Class[]{mapperInterface};
- MapperProxyproxy = new MapperProxy(sqlSession);
- return (T) Proxy.newProxyInstance(classLoader,interfaces, proxy);
- }
publicstatic <T>T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
ClassLoaderclassLoader = mapperInterface.getClassLoader();
Class<?>[] interfaces = new Class[]{mapperInterface};
MapperProxyproxy = new MapperProxy(sqlSession);
return (T) Proxy.newProxyInstance(classLoader,interfaces, proxy);
}
看起來沒什麼特別的,和其他代理類的創建一樣,我們重點關注一下MapperProxy的invoke方法
MapperProxy的invoke
我們知道對被代理對象的方法的訪問都會落實到代理者的invoke上來,MapperProxy的invoke如下:
- public Objectinvoke(Object proxy, Method method, Object[] args) throws Throwable{
- if (method.getDeclaringClass()== Object.class) {
- return method.invoke(this, args);
- }
- finalClass<?> declaringInterface = findDeclaringInterface(proxy, method);
- finalMapperMethod mapperMethod = newMapperMethod(declaringInterface, method, sqlSession);
- final Objectresult = mapperMethod.execute(args);
- if (result ==null && method.getReturnType().isPrimitive()&& !method.getReturnType().equals(Void.TYPE)) {
- thrownewBindingException("Mapper method '" + method.getName() + "'(" + method.getDeclaringClass()
- + ") attempted toreturn null from a method with a primitive return type ("
- + method.getReturnType() + ").");
- }
- return result;
- }
public Objectinvoke(Object proxy, Method method, Object[] args) throws Throwable{
if (method.getDeclaringClass()== Object.class) {
return method.invoke(this, args);
}
finalClass<?> declaringInterface = findDeclaringInterface(proxy, method);
finalMapperMethod mapperMethod = newMapperMethod(declaringInterface, method, sqlSession);
final Objectresult = mapperMethod.execute(args);
if (result ==null && method.getReturnType().isPrimitive()&& !method.getReturnType().equals(Void.TYPE)) {
thrownewBindingException("Mapper method '" + method.getName() + "'(" + method.getDeclaringClass()
+ ") attempted toreturn null from a method with a primitive return type ("
+ method.getReturnType() + ").");
}
return result;
}
可以看到invoke把執行權轉交給了MapperMethod,我們來看看MapperMethod裏又是怎麼運作的:
- public Objectexecute(Object[] args) {
- Objectresult = null;
- if(SqlCommandType.INSERT == type) {
- Objectparam = getParam(args);
- result= sqlSession.insert(commandName, param);
- } elseif(SqlCommandType.UPDATE == type) {
- Object param = getParam(args);
- result= sqlSession.update(commandName, param);
- } elseif(SqlCommandType.DELETE == type) {
- Objectparam = getParam(args);
- result= sqlSession.delete(commandName, param);
- } elseif(SqlCommandType.SELECT == type) {
- if (returnsVoid &&resultHandlerIndex != null) {
- executeWithResultHandler(args);
- } elseif (returnsList) {
- result = executeForList(args);
- } elseif (returnsMap) {
- result = executeForMap(args);
- } else {
- Object param = getParam(args);
- result = sqlSession.selectOne(commandName, param);
- }
- } else {
- thrownewBindingException("Unknown execution method for: " + commandName);
- }
- return result;
- }
public Objectexecute(Object[] args) {
Objectresult = null;
if(SqlCommandType.INSERT == type) {
Objectparam = getParam(args);
result= sqlSession.insert(commandName, param);
} elseif(SqlCommandType.UPDATE == type) {
Object param = getParam(args);
result= sqlSession.update(commandName, param);
} elseif(SqlCommandType.DELETE == type) {
Objectparam = getParam(args);
result= sqlSession.delete(commandName, param);
} elseif(SqlCommandType.SELECT == type) {
if (returnsVoid &&resultHandlerIndex != null) {
executeWithResultHandler(args);
} elseif (returnsList) {
result = executeForList(args);
} elseif (returnsMap) {
result = executeForMap(args);
} else {
Object param = getParam(args);
result = sqlSession.selectOne(commandName, param);
}
} else {
thrownewBindingException("Unknown execution method for: " + commandName);
}
return result;
}
可以看到,MapperMethod就像是一個分發者,他根據參數和返回值類型選擇不同的sqlsession方法來執行。這樣mapper對象與sqlsession就真正的關聯起來了。
Executor
前面提到過,sqlsession只是一個門面,真正發揮作用的是executor,對sqlsession方法的訪問最終都會落到executor的相應方法上去。Executor分成兩大類,一類是CacheExecutor,另一類是普通Executor。Executor的創建前面已經介紹了,下面介紹下他們的功能:
CacheExecutor
CacheExecutor有一個重要屬性delegate,它保存的是某類普通的Executor,值在構照時傳入。執行數據庫update操作時,它直接調用delegate的update方法,執行query方法時先嚐試從cache中取值,取不到再調用delegate的查詢方法,並將查詢結果存入cache中。代碼如下:
- public Listquery(MappedStatement ms, Object parameterObject, RowBounds rowBounds,ResultHandler resultHandler) throws SQLException {
- if (ms != null) {
- Cachecache = ms.getCache();
- if (cache != null) {
- flushCacheIfRequired(ms);
- cache.getReadWriteLock().readLock().lock();
- try {
- if (ms.isUseCache() && resultHandler ==null) {
- CacheKey key = createCacheKey(ms, parameterObject, rowBounds);
- final List cachedList = (List)cache.getObject(key);
- if (cachedList != null) {
- returncachedList;
- } else {
- List list = delegate.query(ms,parameterObject, rowBounds, resultHandler);
- tcm.putObject(cache,key, list);
- return list;
- }
- } else {
- returndelegate.query(ms,parameterObject, rowBounds, resultHandler);
- }
- } finally {
- cache.getReadWriteLock().readLock().unlock();
- }
- }
- }
- returndelegate.query(ms,parameterObject, rowBounds, resultHandler);
- }
public Listquery(MappedStatement ms, Object parameterObject, RowBounds rowBounds,ResultHandler resultHandler) throws SQLException {
if (ms != null) {
Cachecache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
cache.getReadWriteLock().readLock().lock();
try {
if (ms.isUseCache() && resultHandler ==null) {
CacheKey key = createCacheKey(ms, parameterObject, rowBounds);
final List cachedList = (List)cache.getObject(key);
if (cachedList != null) {
returncachedList;
} else {
List list = delegate.query(ms,parameterObject, rowBounds, resultHandler);
tcm.putObject(cache,key, list);
return list;
}
} else {
returndelegate.query(ms,parameterObject, rowBounds, resultHandler);
}
} finally {
cache.getReadWriteLock().readLock().unlock();
}
}
}
returndelegate.query(ms,parameterObject, rowBounds, resultHandler);
}
普通Executor
普通Executor有3類,他們都繼承於BaseExecutor,BatchExecutor專門用於執行批量sql操作,ReuseExecutor會重用statement執行sql操作,SimpleExecutor只是簡單執行sql沒有什麼特別的。下面以SimpleExecutor爲例:
- public ListdoQuery(MappedStatement ms, Object parameter, RowBounds rowBounds,ResultHandler resultHandler) throws SQLException {
- Statementstmt = null;
- try {
- Configuration configuration = ms.getConfiguration();
- StatementHandler handler = configuration.newStatementHandler(this, ms,parameter, rowBounds,resultHandler);
- stmt =prepareStatement(handler);
- returnhandler.query(stmt, resultHandler);
- } finally {
- closeStatement(stmt);
- }
- }
public ListdoQuery(MappedStatement ms, Object parameter, RowBounds rowBounds,ResultHandler resultHandler) throws SQLException {
Statementstmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms,parameter, rowBounds,resultHandler);
stmt =prepareStatement(handler);
returnhandler.query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
可以看出,Executor本質上也是個甩手掌櫃,具體的事情原來是StatementHandler來完成的。
StatementHandler
當Executor將指揮棒交給StatementHandler後,接下來的工作就是StatementHandler的事了。我們先看看StatementHandler是如何創建的。
創建
- publicStatementHandler newStatementHandler(Executor executor, MappedStatementmappedStatement,
- ObjectparameterObject, RowBounds rowBounds, ResultHandler resultHandler) {
- StatementHandler statementHandler = newRoutingStatementHandler(executor, mappedStatement,parameterObject,rowBounds, resultHandler);
- statementHandler= (StatementHandler) interceptorChain.pluginAll(statementHandler);
- returnstatementHandler;
- }
publicStatementHandler newStatementHandler(Executor executor, MappedStatementmappedStatement,
ObjectparameterObject, RowBounds rowBounds, ResultHandler resultHandler) {
StatementHandler statementHandler = newRoutingStatementHandler(executor, mappedStatement,parameterObject,rowBounds, resultHandler);
statementHandler= (StatementHandler) interceptorChain.pluginAll(statementHandler);
returnstatementHandler;
}
可以看到每次創建的StatementHandler都是RoutingStatementHandler,它只是一個分發者,他一個屬性delegate用於指定用哪種具體的StatementHandler。可選的StatementHandler有SimpleStatementHandler、PreparedStatementHandler和CallableStatementHandler三種。選用哪種在mapper配置文件的每個statement裏指定,默認的是PreparedStatementHandler。同時還要注意到StatementHandler是可以被攔截器攔截的,和Executor一樣,被攔截器攔截後的對像是一個代理對象。由於mybatis沒有實現數據庫的物理分頁,衆多物理分頁的實現都是在這個地方使用攔截器實現的,本文作者也實現了一個分頁攔截器,在後續的章節會分享給大家,敬請期待。
初始化
StatementHandler創建後需要執行一些初始操作,比如statement的開啓和參數設置、對於PreparedStatement還需要執行參數的設置操作等。代碼如下:
- private StatementprepareStatement(StatementHandler handler) throwsSQLException {
- Statementstmt;
- Connectionconnection = transaction.getConnection();
- stmt =handler.prepare(connection);
- handler.parameterize(stmt);
- return stmt;
- }
private StatementprepareStatement(StatementHandler handler) throwsSQLException {
Statementstmt;
Connectionconnection = transaction.getConnection();
stmt =handler.prepare(connection);
handler.parameterize(stmt);
return stmt;
}
statement的開啓和參數設置沒什麼特別的地方,handler.parameterize倒是可以看看是怎麼回事。handler.parameterize通過調用ParameterHandler的setParameters完成參數的設置,ParameterHandler隨着StatementHandler的創建而創建,默認的實現是DefaultParameterHandler:
- publicParameterHandler newParameterHandler(MappedStatement mappedStatement, ObjectparameterObject, BoundSql boundSql) {
- ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement,parameterObject,boundSql);
- parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
- returnparameterHandler;
- }
publicParameterHandler newParameterHandler(MappedStatement mappedStatement, ObjectparameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement,parameterObject,boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
returnparameterHandler;
}
同Executor和StatementHandler一樣,ParameterHandler也是可以被攔截的。
參數設置
DefaultParameterHandler裏設置參數的代碼如下:
- publicvoidsetParameters(PreparedStatement ps) throwsSQLException {
- ErrorContext.instance().activity("settingparameters").object(mappedStatement.getParameterMap().getId());
- List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
- if(parameterMappings != null) {
- MetaObject metaObject = parameterObject == null ? null :configuration.newMetaObject(parameterObject);
- for (int i = 0; i< parameterMappings.size(); i++) {
- ParameterMapping parameterMapping = parameterMappings.get(i);
- if(parameterMapping.getMode() != ParameterMode.OUT) {
- Object value;
- String propertyName = parameterMapping.getProperty();
- PropertyTokenizer prop = newPropertyTokenizer(propertyName);
- if (parameterObject == null) {
- value = null;
- } elseif (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())){
- value = parameterObject;
- } elseif (boundSql.hasAdditionalParameter(propertyName)){
- value = boundSql.getAdditionalParameter(propertyName);
- } elseif(propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
- && boundSql.hasAdditionalParameter(prop.getName())){
- value = boundSql.getAdditionalParameter(prop.getName());
- if (value != null) {
- value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
- }
- } else {
- value = metaObject == null ? null :metaObject.getValue(propertyName);
- }
- TypeHandler typeHandler = parameterMapping.getTypeHandler();
- if (typeHandler == null) {
- thrownew ExecutorException("Therewas no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
- }
- typeHandler.setParameter(ps, i + 1, value,parameterMapping.getJdbcType());
- }
- }
- }
- }
publicvoidsetParameters(PreparedStatement ps) throwsSQLException {
ErrorContext.instance().activity("settingparameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if(parameterMappings != null) {
MetaObject metaObject = parameterObject == null ? null :configuration.newMetaObject(parameterObject);
for (int i = 0; i< parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if(parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
PropertyTokenizer prop = newPropertyTokenizer(propertyName);
if (parameterObject == null) {
value = null;
} elseif (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())){
value = parameterObject;
} elseif (boundSql.hasAdditionalParameter(propertyName)){
value = boundSql.getAdditionalParameter(propertyName);
} elseif(propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
&& boundSql.hasAdditionalParameter(prop.getName())){
value = boundSql.getAdditionalParameter(prop.getName());
if (value != null) {
value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
}
} else {
value = metaObject == null ? null :metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
thrownew ExecutorException("Therewas no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
typeHandler.setParameter(ps, i + 1, value,parameterMapping.getJdbcType());
}
}
}
}
這裏面最重要的一句其實就是最後一句代碼,它的作用是用合適的TypeHandler完成參數的設置。那麼什麼是合適的TypeHandler呢,它又是如何決斷出來的呢?BaseStatementHandler的構造方法裏有這麼一句:
this.boundSql= mappedStatement.getBoundSql(parameterObject);
它觸發了sql 的解析,在解析sql的過程中,TypeHandler也被決斷出來了,決斷的原則就是根據參數的類型和參數對應的JDBC類型決定使用哪個TypeHandler。比如:參數類型是String的話就用StringTypeHandler,參數類型是整數的話就用IntegerTypeHandler等。
參數設置完畢後,執行數據庫操作(update或query)。如果是query最後還有個查詢結果的處理過程。
結果處理
結果處理使用ResultSetHandler來完成,默認的ResultSetHandler是FastResultSetHandler,它在創建StatementHandler時一起創建,代碼如下:
- publicResultSetHandler newResultSetHandler(Executor executor, MappedStatementmappedStatement,
- RowBoundsrowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSqlboundSql) {
- ResultSetHandler resultSetHandler =mappedStatement.hasNestedResultMaps() ? newNestedResultSetHandler(executor, mappedStatement, parameterHandler,resultHandler, boundSql, rowBounds): new FastResultSetHandler(executor,mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
- resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
- returnresultSetHandler;
- }
publicResultSetHandler newResultSetHandler(Executor executor, MappedStatementmappedStatement,
RowBoundsrowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSqlboundSql) {
ResultSetHandler resultSetHandler =mappedStatement.hasNestedResultMaps() ? newNestedResultSetHandler(executor, mappedStatement, parameterHandler,resultHandler, boundSql, rowBounds): new FastResultSetHandler(executor,mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
returnresultSetHandler;
}
可以看出ResultSetHandler也是可以被攔截的,可以編寫自己的攔截器改變ResultSetHandler的默認行爲。
- ResultSetHandler內部一條記錄一條記錄的處理,在處理每條記錄的每一列時會調用TypeHandler轉換結果,如下:
- protectedbooleanapplyAutomaticMappings(ResultSet rs, List<String> unmappedColumnNames,MetaObject metaObject) throws SQLException {
- booleanfoundValues = false;
- for (StringcolumnName : unmappedColumnNames) {
- final Stringproperty = metaObject.findProperty(columnName);
- if (property!= null) {
- final ClasspropertyType =metaObject.getSetterType(property);
- if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
- final TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(propertyType);
- final Object value = typeHandler.getResult(rs,columnName);
- if (value != null) {
- metaObject.setValue(property, value);
- foundValues = true;
- }
- }
- }
- }
- returnfoundValues;
- }
ResultSetHandler內部一條記錄一條記錄的處理,在處理每條記錄的每一列時會調用TypeHandler轉換結果,如下:
protectedbooleanapplyAutomaticMappings(ResultSet rs, List<String> unmappedColumnNames,MetaObject metaObject) throws SQLException {
booleanfoundValues = false;
for (StringcolumnName : unmappedColumnNames) {
final Stringproperty = metaObject.findProperty(columnName);
if (property!= null) {
final ClasspropertyType =metaObject.getSetterType(property);
if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
final TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(propertyType);
final Object value = typeHandler.getResult(rs,columnName);
if (value != null) {
metaObject.setValue(property, value);
foundValues = true;
}
}
}
}
returnfoundValues;
}
從代碼裏可以看到,決斷TypeHandler使用的是結果參數的屬性類型。因此我們在定義作爲結果的對象的屬性時一定要考慮與數據庫字段類型的兼容性。