Mybatis(四):MyBatis核心组件介绍原理解析和源码解读

Mybatis(四):MyBatis核心组件介绍原理解析和源码解读

Mybatis核心成员

Configuration MyBatis所有的配置信息都保存在Configuration对象之中,配置文件中的大部分配置都会存储到该类中

SqlSession 作为MyBatis工作的主要顶层API,表示和数据库交互时的会话,完成必要数据库增删改查功能

Executor MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护

StatementHandler 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数等

ParameterHandler 负责对用户传递的参数转换成JDBC Statement 所对应的数据类型

ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合

TypeHandler 负责java数据类型和jdbc数据类型(也可以说是数据表列类型)之间的映射和转换

MappedStatement MappedStatement维护一条节点的封装

SqlSource 负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回

BoundSql 表示动态生成的SQL语句以及相应的参数信息

以上主要成员在一次数据库操作中基本都会涉及,在SQL操作中重点需要关注的是SQL参数什么时候被设置和结果集怎么转换为JavaBean对象的,这两个过程正好对应StatementHandler和ResultSetHandler类中的处理逻辑。

MyBatis启动原理和源码解析

1. SqlSessionFactory 与 SqlSession.

通过前面的章节对于mybatis 的介绍及使用,大家都能体会到SqlSession的重要性了吧, 没错,从表面上来看,咱们都是通过SqlSession去执行sql语句(注意:是从表面看,实际的待会儿就会讲)。那么咱们就先看看是怎么获取SqlSession的吧:

(1)首先,SqlSessionFactoryBuilder去读取mybatis的配置文件,然后build一个DefaultSqlSessionFactory。

String resource = "mybatis.xml";

// 加载mybatis的配置文件(它也加载关联的映射文件)

InputStream inputStream = null;

try {

inputStream = Resources.getResourceAsStream(resource);

} catch (IOException e) {

e.printStackTrace();

}

// 构建sqlSession的工厂

sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

源码如下,首先会创建SqlSessionFactory建造者对象,然后由它进行创建SqlSessionFactory。这里用到的是建造者模式,建造者模式最简单的理解就是不手动new对象,而是由其他类来进行对象的创建。更多建造模式学习参阅:设计模式之建造者模式学习理解

// SqlSessionFactoryBuilder类

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.

}

}

}

通过以上步骤,咱们已经得到SqlSession对象了。接下来就是该干嘛干嘛去了(话说还能干嘛,当然是执行sql语句咯)。

SqlSession咱们也拿到了,咱们可以调用SqlSession中一系列的select..., insert..., update..., delete...方法轻松自如的进行CRUD操作了。 就这样? 那咱配置的映射文件去哪儿了? 别急, 咱们接着往下看:

2. 利器之MapperProxy:

在mybatis中,通过MapperProxy动态代理咱们的dao, 也就是说, 当咱们执行自己写的dao里面的方法的时候,其实是对应的mapperProxy在代理。那么,咱们就看看怎么获取MapperProxy对象吧:

(1)通过SqlSession从Configuration中获取。源码如下:

/**

* 什么都不做,直接去configuration中找, 哥就是这么任性

*/

@Override

public T getMapper(Class type) {

return configuration.getMapper(type, this);

}

(2)SqlSession把包袱甩给了Configuration, 接下来就看看Configuration。源码如下:

/**

* 烫手的山芋,俺不要,你找mapperRegistry去要

* @param type

* @param sqlSession

* @return

*/

public T getMapper(Class type, SqlSession sqlSession) {

return mapperRegistry.getMapper(type, sqlSession);

}

(3)Configuration不要这烫手的山芋,接着甩给了MapperRegistry, 那咱看看MapperRegistry。 源码如下:

/**

* 烂活净让我来做了,没法了,下面没人了,我不做谁来做

* @param type

* @param sqlSession

* @return

*/

@SuppressWarnings("unchecked")

public T getMapper(Class type, SqlSession sqlSession) {

//能偷懒的就偷懒,俺把粗活交给MapperProxyFactory去做

final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) 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的人,粗活最终交给它去做了。咱们看看源码:

/**

* 别人虐我千百遍,我待别人如初恋

* @param mapperProxy

* @return

*/

@SuppressWarnings("unchecked")

protected T newInstance(MapperProxy mapperProxy) {

//动态代理我们写的dao接口

return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);

}

public T newInstance(SqlSession sqlSession) {

final MapperProxy mapperProxy = new MapperProxy(sqlSession, mapperInterface, methodCache);

return newInstance(mapperProxy);

}

这里是关键,通过以上的动态代理(更多动态代理知识参阅:java中代理,静态代理,动态代理以及spring aop代理方式,实现原理统一汇总 这篇文章里的动态代理章节),咱们就可以方便地使用dao接口啦, 就像之前咱们写的demo那样:

UserDao userMapper = sqlSession.getMapper(UserDao.class);

User insertUser = new User();

这下方便多了吧, 呵呵, 貌似mybatis的源码就这么一回事儿啊。

别急,还没完, 咱们还没看具体是怎么执行sql语句的呢。

3. Excutor:

接下来,咱们才要真正去看sql的执行过程了。

上面,咱们拿到了MapperProxy, 每个MapperProxy对应一个dao接口, 那么咱们在使用的时候,MapperProxy是怎么做的呢? 源码奉上:

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);

//二话不说,主要交给MapperMethod自己去管

return mapperMethod.execute(sqlSession, args);

}

MapperMethod:

/**

* 看着代码不少,不过其实就是先判断CRUD类型,然后根据类型去选择到底执行sqlSession中的哪个方法,绕了一圈,又转回sqlSession了

* @param sqlSession

* @param args

* @return

*/

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 List selectList(String statement, Object parameter, RowBounds rowBounds) {

try {

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 List 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());

//StatementHandler封装了Statement, 让 StatementHandler 去处理

return handler.query(stmt, resultHandler);

} finally {

closeStatement(stmt);

}

}

接下来,咱们看看StatementHandler 的一个实现类 PreparedStatementHandler(这也是我们最常用的,封装的是PreparedStatement), 看看它使怎么去处理的:

public List query(Statement statement, ResultHandler resultHandler) throws SQLException {

//到此,原形毕露, PreparedStatement, 这个大家都已经滚瓜烂熟了吧

PreparedStatement ps = (PreparedStatement) statement;

ps.execute();

//结果交给了ResultSetHandler 去处理

return resultSetHandler. handleResultSets(ps);

}

到此, 一次sql的执行流程就完了。

相关推荐

【Oracle笔记】详解表分区的方式(范围、散列、列表、复合)
一、什么是诈骗罪?
365bet欧洲版官网

一、什么是诈骗罪?

⏱️ 10-02 👁️ 2206
微信名怎么设置成空白格式
365bet欧洲版官网

微信名怎么设置成空白格式

⏱️ 07-10 👁️ 4309
eb病毒感染多久能好
365bet欧洲版官网

eb病毒感染多久能好

⏱️ 07-18 👁️ 8214
开路是什么意思
365bet官方博客

开路是什么意思

⏱️ 10-29 👁️ 705
【原】电压力锅哪个品牌的质量最好?10 款双变压,炖肉煮饭不串味!