最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅。在前九篇中,介绍了mybatis的配置以及使用, 那么本篇将走进mybatis的源码,分析mybatis 的执行流程, 好啦,鄙人不喜欢口水话,还是直接上干活吧:
1. SqlSessionFactory 与 SqlSession.
通过前面的章节对于mybatis 的介绍及使用,大家都能体会到SqlSession的重要性了吧, 没错,从表面上来看,咱们都是通过SqlSession去执行sql语句(注意:是从表面看,实际的待会儿就会讲)。那么咱们就先看看是怎么获取SqlSession的吧:
(1)首先,SqlSessionFactoryBuilder去读取mybatis的配置文件,然后build一个DefaultSqlSessionFactory。源码如下:
public SqlSessionFactory build ( Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder ( reader, environment, properties) ;
return build ( parser. parse ( ) ) ;
} catch ( Exception e) {
throw ExceptionFactory. wrapException ( "Error building SqlSession." , e) ;
} finally {
ErrorContext. instance ( ) . reset ( ) ;
try {
reader. close ( ) ;
} catch ( IOException e) {
}
}
}
public SqlSessionFactory build ( Configuration config) {
return new DefaultSqlSessionFactory ( config) ;
}
(2)当我们获取到SqlSessionFactory之后,就可以通过SqlSessionFactory去获取SqlSession对象。源码如下:
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) ;
throw ExceptionFactory. wrapException ( "Error opening session. Cause: " + e, e) ;
} finally {
ErrorContext. instance ( ) . reset ( ) ;
}
}
通过以上步骤,咱们已经得到SqlSession对象了。接下来就是该干嘛干嘛去了(话说还能干嘛,当然是执行sql语句咯)。看了上面,咱们也回想一下之前写的Demo,
SqlSessionFactory sessionFactory = null;
String resource = "mybatis-conf.xml" ;
try {
sessionFactory = new SqlSessionFactoryBuilder ( ) . build ( Resources
. getResourceAsReader ( resource) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
SqlSession sqlSession = sessionFactory. openSession ( ) ;
还真这么一回事儿,对吧!
SqlSession咱们也拿到了,咱们可以调用SqlSession中一系列的select..., insert..., update..., delete...方法轻松自如的进行CRUD操作了。 就这样? 那咱配置的映射文件去哪儿了? 别急, 咱们接着往下看:
2. 利器之MapperProxy:
在mybatis中,通过MapperProxy动态代理咱们的dao, 也就是说, 当咱们执行自己写的dao里面的方法的时候,其实是对应的mapperProxy在代理。那么,咱们就看看怎么获取MapperProxy对象吧:
(1)通过SqlSession从Configuration中获取。源码如下:
@Override
public < T> T getMapper ( Class< T> type) {
return configuration. < T> getMapper ( type, this ) ;
}
(2)SqlSession把包袱甩给了Configuration, 接下来就看看Configuration。源码如下:
public < T> T getMapper ( Class< T> type, SqlSession sqlSession) {
return mapperRegistry. getMapper ( type, sqlSession) ;
}
(3)Configuration不要这烫手的山芋,接着甩给了MapperRegistry, 那咱看看MapperRegistry。 源码如下:
@SuppressWarnings ( "unchecked" )
public < T> T getMapper ( Class< T> type, SqlSession sqlSession) {
final MapperProxyFactory< T> mapperProxyFactory = ( MapperProxyFactory< T> ) knownMappers. get ( type) ;
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) ;
}
}
(4)MapperProxyFactory是个苦B的人,粗活最终交给它去做了。咱们看看源码:
@SuppressWarnings ( "unchecked" )
protected T newInstance ( MapperProxy< T> mapperProxy) {
return ( T) Proxy. newProxyInstance ( mapperInterface. getClassLoader ( ) , new Class [ ] { mapperInterface } , mapperProxy) ;
}
public T newInstance ( SqlSession sqlSession) {
final MapperProxy< T> mapperProxy = new MapperProxy < T> ( sqlSession, mapperInterface, methodCache) ;
return newInstance ( mapperProxy) ;
}
通过以上的动态代理,咱们就可以方便地使用dao接口啦, 就像之前咱们写的demo那样:
UserDao userMapper = sqlSession. getMapper ( UserDao. class ) ;
User insertUser = new User ( ) ;
这下方便多了吧, 呵呵, 貌似mybatis的源码就这么一回事儿啊。
别急,还没完, 咱们还没看具体是怎么执行sql语句的呢。
3. Excutor:
接下来,咱们才要真正去看sql的执行过程了。
上面,咱们拿到了MapperProxy, 每个MapperProxy对应一个dao接口, 那么咱们在使用的时候,MapperProxy是怎么做的呢? 源码奉上:
MapperProxy:
@Override
public Object invoke ( Object proxy, Method method, Object[ ] args) throws Throwable {
if ( Object. class . equals ( method. getDeclaringClass ( ) ) ) {
try {
return method. invoke ( this , args) ;
} catch ( Throwable t) {
throw ExceptionUtil. unwrapThrowable ( t) ;
}
}
final MapperMethod mapperMethod = cachedMapperMethod ( method) ;
return mapperMethod. execute ( sqlSession, args) ;
}
MapperMethod:
public Object execute ( SqlSession sqlSession, Object[ ] args) {
Object result;
if ( SqlCommandType. INSERT == command. getType ( ) ) {
Object param = method. convertArgsToSqlCommandParam ( args) ;
result = rowCountResult ( sqlSession. insert ( command. getName ( ) , param) ) ;
} else if ( SqlCommandType. UPDATE == command. getType ( ) ) {
Object param = method. convertArgsToSqlCommandParam ( args) ;
result = rowCountResult ( sqlSession. update ( command. getName ( ) , param) ) ;
} else if ( SqlCommandType. DELETE == command. getType ( ) ) {
Object param = method. convertArgsToSqlCommandParam ( args) ;
result = rowCountResult ( sqlSession. delete ( command. getName ( ) , param) ) ;
} else if ( SqlCommandType. SELECT == command. getType ( ) ) {
if ( method. returnsVoid ( ) && method. hasResultHandler ( ) ) {
executeWithResultHandler ( sqlSession, args) ;
result = null;
} else if ( method. returnsMany ( ) ) {
result = executeForMany ( sqlSession, args) ;
} else if ( method. returnsMap ( ) ) {
result = executeForMap ( sqlSession, args) ;
} else {
Object param = method. convertArgsToSqlCommandParam ( args) ;
result = sqlSession. selectOne ( command. getName ( ) , param) ;
}
} else {
throw new BindingException ( "Unknown execution method for: " + command. getName ( ) ) ;
}
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;
}
既然又回到SqlSession了, 那么咱们就看看SqlSession的CRUD方法了,为了省事,还是就选择其中的一个方法来做分析吧。这儿,咱们选择了selectList方法:
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 ( ) ;
}
}
然后,通过一层一层的调用,最终会来到doQuery方法, 这儿咱们就随便找个Excutor看看doQuery方法的实现吧,我这儿选择了SimpleExecutor:
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 的一个实现类 PreparedStatementHandler(这也是我们最常用的,封装的是PreparedStatement), 看看它使怎么去处理的:
public < E> List< E> query ( Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = ( PreparedStatement) statement;
ps. execute ( ) ;
return resultSetHandler. < E> handleResultSets ( ps) ;
}
到此, 一次sql的执行流程就完了。 我这儿仅抛砖引玉,建议有兴趣的去看看Mybatis3的源码。
好啦,本次就到此结束啦,最近太忙了, 又该忙去啦。