初看mybatis sql执行时间源码 SQL是怎么执行的

Mapper中的方法执行时会构造为org.apache.ibatis.binding.MapperMethod$MethodSignature对象,从该类源码中可以了解如何使用Mapper方法。
【支持的特殊参数类型】
RowBounds、ResultHandler、普通参数&(作为sql执行时使用的变量)
其中普通参数可以是单一的model、查询条件的map或直接将一到多个查询条件作为参数(多个条件在框架中最终将封装为Map&String, Object&使用)
另外普通参数支持添加@Param注解以修改参数名,如不修改则参数名用0、1、2来表示。
使用时,如参数有多个则只能使用0和param1这两种形式。(如:SELECT * FROM TABLE WHERE COLUMN=#{0})
相关源码:&
public Object convertArgsToSqlCommandParam(Object[] args) {
final int paramCount = params.size();
if (args == null || paramCount == 0) {
return null;
} else if (!hasNamedParameters && paramCount == 1) {
return args[params.keySet().iterator().next()];
final Map&String, Object& param = new ParamMap&Object&();
int i = 0;
for (Map.Entry&Integer, String& entry : params.entrySet()) {
param.put(entry.getValue(), args[entry.getKey()]);
// issue #71, add param names as param1, param2...but ensure backward compatibility
final String genericParamName = "param" + String.valueOf(i + 1);
if (!param.containsKey(genericParamName)) {
param.put(genericParamName, args[entry.getKey()]);
【支持的返回类型】
Model类型、void、集合、数组皆可
需注意的是如果返回类型为map则需添加注解@MapKey(不加MethodSignature类中returnsMap属性会被设为false,其他后果未验证)
【在SelectProvider中修改参数】
Mybatis框架进入SelectProvider在前述convertArgsToSqlCommandParam方法之后,最终执行sql之前,因此可以在此处对最终执行sql的参数进行修改(前提是不能修改参数引用)。如使用参数Map&String, Object&的形式,就更可以灵活地进行修改。
注:多个参数必须使用Map接收,单个参数可以使用原有类型接收也可以使用Map接收。
阅读(...) 评论()Mybatis从配置读取到打开连接的源码分析_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Mybatis从配置读取到打开连接的源码分析
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩5页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢1.先获得SqlSessionFactory
& & & & &通过SqlSessionFactoryBuilder去读取mybatis的配置文件,然后build一个DefaultSqlSessionFactory。
* 一系列的构造方法最终都会调用本方法(配置文件为Reader时会调用本方法,还有一个InputStream方法与此对应)
* @param reader
* @param environment
* @param properties
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
//通过XMLConfigBuilder解析配置文件,解析的配置相关信息都会封装为一个Configuration对象
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
//这儿创建DefaultSessionFactory对象
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException(&Error building SqlSession.&, e);
} finally {
ErrorContext.instance().reset();
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
2.通过SqlSessionFactory去获取SqlSession对象
public SqlSession openSession()
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
Transaction tx =
//通过Configuration对象去获取相关的配置信息,Environment对象包含了数据源和事务的配置
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//实际上就是Executor来执行sql语句,executor是对于Statement的封装
final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
//创建DefaultSqlSession对象
return new DefaultSqlSession(configuration, executor);
catch (Exception e)
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException(&Error opening session.
Cause: & + e, e);
ErrorContext.instance().reset();
}& & & & & & & & & & & 这样就获得了SqlSession对象
3.通过SqlSession获得Mapper
public &T& T getMapper(Class&T& type)
return configuration.&T&getMapper(type, this);
}& & & & & & 这里通过Configuration的getMapper方法来获得
public &T& T getMapper(Class&T& type, SqlSession sqlSession)
return mapperRegistry.getMapper(type, sqlSession);
protected MapperRegistry mapperRegistry = new MapperRegistry(this);
& & &继续看MapperRegistry的getMapper方法
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.&);
return mapperProxyFactory.newInstance(sqlSession);
//此处使用反射,生成一个MapperProxyFactory对象
catch (Exception e)
throw new BindingException(&Error getting mapper instance. Cause: & + e, e);
private final Map&Class&?&, MapperProxyFactory&?&& knownMappers = new HashMap&Class&?&, MapperProxyFactory&?&&();&span style=&font-family: Arial, Helvetica, sans- font-size: 12 background-color: rgb(255, 255, 255);&&            &/span&
& & & & &knownMappers在读取配置文件的时候,已经将mapper信息放入其中了。
& & & & 继续看MapperProxyFactory的newInstance方法:
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);
}& & & & MapperProxy就是一个代理类,使用的是jdk的动态代理
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
if (Object.class.equals(method.getDeclaringClass()))
return method.invoke(this, args);
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}& & & &这里的invoke方法可以明确看出使用的是jdk的动态代理
& & & 这样的话就会返回一个mapper的代理类,每次执行的时候,回交给MapperMethod类的execute方法
public Object execute(SqlSession sqlSession, Object[] args)
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);
else if (method.returnsMany())
result = executeForMany(sqlSession, args);
else if (method.returnsMap())
result = executeForMap(sqlSession, args);
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
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() + &).&);
}& & & & & & & &可以看出,也就是判断一下CURD的类型,然后实际调用的还是SqlSession 的方法,在这里以selectList为例
public &E& List&E& selectList(String statement, Object parameter, RowBounds rowBounds) {
MappedStatement ms = configuration.getMappedStatement(statement);
List&E& result = 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的query方法,在这里使用BaseExecutor的
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);
}& & & & &继续查找到SimpleExecutor的doQuery,可以看到
public &E& List&E& doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException
Statement stmt =
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.&E&query(stmt, resultHandler);
closeStatement(stmt);
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection);
handler.parameterize(stmt);
}& & & 可以看出实际上也就好似调用的jdbc
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37991次
排名:千里之外
转载:331篇
(17)(45)(19)(33)(22)(3)(1)(7)(43)(46)(10)(4)(8)(20)(17)(13)(2)(20)(10)&&&&&&&&&&&
最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅。在前九篇中,介绍了mybatis的配置以及使用, 那么本篇将走进mybatis的源码,分析mybatis 的执行流程, 好啦,鄙人不喜欢口水话,还是直接上干活吧:
1. SqlSessionFactory 与 SqlSession.
  通过前面的章节对于mybatis 的介绍及使用,大家都能体会到SqlSession的重要性了吧, 没错,从表面上来看,咱们都是通过SqlSession去执行sql语句(注意:是从表面看,实际的待会儿就会讲)。那么咱们就先看看是怎么获取SqlSession的吧:
(1)首先,SqlSessionFactoryBuilder去读取mybatis的配置文件,然后build一个DefaultSqlSessionFactory。源码如下:
* 一系列的构造方法最终都会调用本方法(配置文件为Reader时会调用本方法,还有一个InputStream方法与此对应)
* @param reader
* @param environment
* @param properties
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
//通过XMLConfigBuilder解析配置文件,解析的配置相关信息都会封装为一个Configuration对象
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
//这儿创建DefaultSessionFactory对象
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
(2)当我们获取到SqlSessionFactory之后,就可以通过SqlSessionFactory去获取SqlSession对象。源码如下:
* 通常一系列openSession方法最终都会调用本方法
* @param execType
* @param level
* @param autoCommit
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
//通过Confuguration对象去获取Mybatis相关配置信息, Environment对象包含了数据源和事务的配置
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//之前说了,从表面上来看,咱们是用sqlSession在执行sql语句, 实际呢,其实是通过excutor执行, excutor是对于Statement的封装
final Executor executor = configuration.newExecutor(tx, execType);
//关键看这儿,创建了一个DefaultSqlSession对象
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对象了。接下来就是该干嘛干嘛去了(话说还能干嘛,当然是执行sql语句咯)。看了上面,咱们也回想一下之前写的Demo,&
SqlSessionFactory sessionFactory = null;
String resource = "mybatis-conf.xml";
//SqlSessionFactoryBuilder读取配置文件
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
//通过SqlSessionFactory获取SqlSessionSqlSession sqlSession = sessionFactory.openSession();
还真这么一回事儿,对吧!&
SqlSession咱们也拿到了,咱们可以调用SqlSession中一系列的select..., &insert..., update..., delete...方法轻松自如的进行CRUD操作了。 就这样? 那咱配置的映射文件去哪儿了? &别急, 咱们接着往下看:
2. 利器之MapperProxy:
在mybatis中,通过MapperProxy动态代理咱们的dao, 也就是说, 当咱们执行自己写的dao里面的方法的时候,其实是对应的mapperProxy在代理。那么,咱们就看看怎么获取MapperProxy对象吧:
(1)通过SqlSession从Configuration中获取。源码如下:
* 什么都不做,直接去configuration中找, 哥就是这么任性
public &T& T getMapper(Class&T& type) {
return configuration.&T&getMapper(type, this);
(2)SqlSession把包袱甩给了Configuration, 接下来就看看Configuration。源码如下:
* 烫手的山芋,俺不要,你找mapperRegistry去要
* @param type
* @param sqlSession
public &T& T getMapper(Class&T& type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
(3)Configuration不要这烫手的山芋,接着甩给了MapperRegistry, 那咱看看MapperRegistry。 源码如下:
* 烂活净让我来做了,没法了,下面没人了,我不做谁来做
* @param type
* @param sqlSession
@SuppressWarnings("unchecked")
public &T& T getMapper(Class&T& type, SqlSession sqlSession) {
//能偷懒的就偷懒,俺把粗活交给MapperProxyFactory去做
final MapperProxyFactory&T& mapperProxyFactory = (MapperProxyFactory&T&) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
//关键在这儿
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
(4)MapperProxyFactory是个苦B的人,粗活最终交给它去做了。咱们看看源码:
* 别人虐我千百遍,我待别人如初恋
* @param mapperProxy
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy&T& mapperProxy) {
//动态代理我们写的dao接口
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:
* MapperProxy在执行时会触发此方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
final MapperMethod mapperMethod = cachedMapperMethod(method);
//二话不说,主要交给MapperMethod自己去管
return mapperMethod.execute(sqlSession, args);
MapperMethod:
* 看着代码不少,不过其实就是先判断CRUD类型,然后根据类型去选择到底执行sqlSession中的哪个方法,绕了一圈,又转回sqlSession了
* @param sqlSession
* @param args
public Object execute(SqlSession sqlSession, Object[] args) {
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);
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
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() + ").");
既然又回到SqlSession了, 那么咱们就看看SqlSession的CRUD方法了,为了省事,还是就选择其中的一个方法来做分析吧。这儿,咱们选择了selectList方法:
public &E& List&E& selectList(String statement, Object parameter, RowBounds rowBounds) {
MappedStatement ms = configuration.getMappedStatement(statement);
//CRUD实际上是交给Excetor去处理, excutor其实也只是穿了个马甲而已,小样,别以为穿个马甲我就不认识你嘞!
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;
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
//StatementHandler封装了Statement, 让 StatementHandler 去处理
return handler.&E&query(stmt, resultHandler);
} finally {
closeStatement(stmt);
接下来,咱们看看StatementHandler 的一个实现类&PreparedStatementHandler(这也是我们最常用的,封装的是PreparedStatement), 看看它使怎么去处理的:
public &E& List&E& query(Statement statement, ResultHandler resultHandler) throws SQLException {
//到此,原形毕露, PreparedStatement, 这个大家都已经滚瓜烂熟了吧
PreparedStatement ps = (PreparedStatement)
ps.execute();
//结果交给了ResultSetHandler 去处理
return resultSetHandler.&E& handleResultSets(ps);
到此, 一次sql的执行流程就完了。 我这儿仅抛砖引玉,建议有兴趣的去看看Mybatis3的源码。
好啦,本次就到此结束啦,最近太忙了, 又该忙去啦。
阅读(...) 评论()

我要回帖

更多关于 mybatis获取执行的sql 的文章

 

随机推荐