SSM是目前常见的构建Web项目的方案,Mybatis是其中重要的一环,如果能深刻的理解Mybatis的内部原理,对我们会有极大的帮助,接下来一起看看Mybatis的内部设计。
准备
-
搭建Mybatis的基本运行环境,参考Mybatis入门
- 贴上自己的代码
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 查询数据库内容
sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
System.out.println(user);
// 插入数据库内容
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
分析
- 获取Mybatis的配置文件,内部通过ClassLoader加载文件流,这一步需要对Classloader有一定的理解,里面相对简单,就不多说了
Resources.getResourceAsStream("mybatis.xml");
- 创建SqlSessionFactory, 通过JDK内部的w3c解析配置文件的内容,封装到Configration对象中,最后通过Configuration来创建DefaultSqlSessionFactory.
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);
}
- 通过SqlSessionFactory创建SqlSession对象
- 获取配置的env参数,在解析配置的时候根据配置生成了TransactionFactory
- TransactionFactory对象,在设置env后,默认是JDBCTransactionFactory
- 通过配置生成默认的executor,executor是很重要的组件,可以看到executor封装了很多操作数据库相关的东西
- 生成默认的DefaultSqlSession,内部有executor,TransactionFactory,configuration
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
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); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
- SqlSession通过key获取到与key绑定的sql语句,并且执行,最后获取到结果。这步内部可以细分
sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
selectOne内部调用的是selectList的函数,selectList函数内部,首先获取到key对应的MappedStatement,然后通过Executor查询MapperStatement。
在解析配置的时候建立key与MappedStatement映射关系的
@Override
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();
}
}
- 不同的executor内部的查询方法不同,后面我们详细讲解不同的executor。这次先看SimpleExecutor.
- queryStack判断当前的SQL执行栈,可能会连续执行多条sql语句
- localCache 缓存当前Sqlsession查询到的对象
- queryFromDatabase 如果没有缓存从数据库中进行查询,重点语句
@Override
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);
}
@SuppressWarnings("unchecked")
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List<E> list;
try {
queryStack++;
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
if (list != null) {
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
// issue #601
deferredLoads.clear();
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
// issue #482
clearLocalCache();
}
}
return list;
}
- query方法中真正的查询交给子类的doQuery,而SimpleExecutor的doQuery如下
- 新建StatementHandler,语句处理器
- 通过StatementHandler再进行query
- Statement为JDK提供的SQL接口,获取Statement经历了StatementHandler.prepare,然后parameterize设置参数。
- query方法利用MySQL内部的query
- 获取到结果之后,通过resultHandler处理获取到的结果
@Override
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内部的调用,结果集处理使用的是DefaultResultSetHandler,内部ResultSet的处理属于JDBC的知识,想看懂Mybatis,对JDBC也要有一定的理解
- 获取到结果返回给我们
User user = sqlSession.selectOne("me.aihe.dao.UserMapper.selectUser",1);
回顾
这次大致看了下Mybatis的基本执行流程,涉及到了几个关键的类
- SqlSessionFactory ,SqlSession
- MappedStatement 封装我们写的SQL语句
- StatementHandler 语句处理器
- ResultHandler 返回的结果处理器
- Executor Mybatis中的语句执行器
创建SqlSessionFactory -> 获取SqlSession -> 获取->MappedStatement -> 获取StatementHandler同时创建Statement -> 执行Statement -> 使用ResultSet处理执行的结果,处理结果根据Mapperr.xml文件中指定的类型映射最终实现ORM功能
总结
Mybatis的过程相比Spring MVC更直观一些,不过需要熟悉JDBC的知识,内部还有一些细节,后续继续研究