mybatis的源码分析

  • 一、传统方式源码分析
  • 二、Mapper代理方式源码分析
  • 三、MyBatis源码中涉及到的设计模式

一、传统方式源码分析

分析之前我们来回顾下传统方式的写法:

/*** 传统方式*/
public void test1() throws IOException {// 1. 读取配置文件,读成字节输入流,注意:现在还没解析InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");// 2. 解析配置文件,封装Configuration对象   创建DefaultSqlSessionFactory对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);// 3. 生产了DefaultSqlsession实例对象   设置了事务不自动提交  完成了executor对象的创建SqlSession sqlSession = sqlSessionFactory.openSession();// 4.(1)根据statementid来从Configuration中map集合中获取到了指定的MappedStatement对象//   (2)将查询任务委派了executor执行器List<Object> objects = sqlSession.selectList("namespace.id");// 5.释放资源sqlSession.close();}

1、初始化

// 1. 读取配置文件,读成字节输入流,注意:现在还没解析
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");// 2. 解析配置文件,封装Configuration对象   创建DefaultSqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

初始化操作也就是mybatis框架会读取到我们本地项目中resourcesmybatis-config.xml配置文件。
然后开始解析配置文件!!!

废话不多说,直接跟我走进源码的世界。

// 1.我们最初调用的build
public SqlSessionFactory build(InputStream inputStream) {//调用了重载方法return build(inputStream, null, null);
}
// 2.调用的重载方法
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {try {// 创建 XMLConfigBuilder, XMLConfigBuilder是专门解析mybatis的配置文件的类XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);// 执行 XML 解析// 创建 DefaultSqlSessionFactory 对象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.}}
}

MyBatis在初始化的时候,会将MyBatis的配置信息全部加载到内存中,使用org.apache.ibatis.session.Configuration实例来维护。

Configuration这个类非常重要!!!它是后续解析MyBatis的核心配置文件以及Mapper.xml的关键。

这样跟你说吧:Configuration对象的结构和xml配置文件的对象几乎相同。

我们继续跟上面的parser.parse()

/*** 解析 XML 成 Configuration 对象。** @return Configuration 对象*/
public Configuration parse() {// 若已解析,抛出 BuilderException 异常if (parsed) {throw new BuilderException("Each XMLConfigBuilder can only be used once.");}// 标记已解析parsed = true;///parser是XPathParser解析器对象,读取节点内数据,<configuration>是MyBatis配置文件中的顶层标签// 解析 XML configuration 节点parseConfiguration(parser.evalNode("/configuration"));return configuration;
}/*** 解析 XML** 具体 MyBatis 有哪些 XML 标签,参见 《XML 映射配置文件》http://www.mybatis.org/mybatis-3/zh/configuration.html** @param root 根节点*/
private void parseConfiguration(XNode root) {try {//issue #117 read properties first// 解析 <properties /> 标签propertiesElement(root.evalNode("properties"));// 解析 <settings /> 标签Properties settings = settingsAsProperties(root.evalNode("settings"));// 加载自定义的 VFS 实现类loadCustomVfs(settings);// 解析 <typeAliases /> 标签typeAliasesElement(root.evalNode("typeAliases"));// 解析 <plugins /> 标签pluginElement(root.evalNode("plugins"));// 解析 <objectFactory /> 标签objectFactoryElement(root.evalNode("objectFactory"));// 解析 <objectWrapperFactory /> 标签objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));// 解析 <reflectorFactory /> 标签reflectorFactoryElement(root.evalNode("reflectorFactory"));// 赋值 <settings /> 到 Configuration 属性settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631// 解析 <environments /> 标签environmentsElement(root.evalNode("environments"));// 解析 <databaseIdProvider /> 标签databaseIdProviderElement(root.evalNode("databaseIdProvider"));// 解析 <typeHandlers /> 标签typeHandlerElement(root.evalNode("typeHandlers"));// 解析 <mappers /> 标签mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}
}

这里介绍下MappedStatement,这也是一个关键的类。为啥这样说呢?下面我们来看下它的作用你就知道了:

Configuration实例中维护了mappedStatements这个变量,这是一个键值对的Map集合,keynamespace.id,valueMappedStatement

MappedStatementMapper配置文件中的一个select/update/delete/insert节点对应,mapper的配置文件都封装到了此对象中,主要用途是描述一条sql语句。

初始化过程:回顾刚开始介绍的加载配置文件的过程中,会对mybatis-config.xml中的各个标签都进行解析,其中mappers标签用来引入mapper.xml文件或者配置mapper接口的目录。

<select id="findAll" resultType="com.riemann.pojo.User">select * from user
</select>

比如上面这个select标签会在初始化配置文件时被解析成一个MappedStatement对象,然后存储在Configuration对象的mappedStatements属性中,mappedStatements是一个HashMap,存储时key=全限定类名+方法名value=对应的MappedStatement对象

  • Configuration中对应属性为

    /*** MappedStatement 映射** KEY:`${namespace}.${id}`*/
    protected final Map<String, MappedStatement> mappedStatements = new StrictMap<>("Mapped Statements collection");
    
  • XMLConfigBuilder中的处理:

    /**
    * 解析 XML
    *
    * 具体 MyBatis 有哪些 XML 标签,参见 《XML 映射配置文件》http://www.mybatis.org/mybatis-3/zh/configuration.html
    *
    * @param root 根节点
    */
    private void parseConfiguration(XNode root) {try {...// 解析 <mappers /> 标签mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}
    }
    

我们这里继续跟解析mappers标签mapperElement(root.evalNode("mappers"));,其他的解析这里就不展示了哈。

private void mapperElement(XNode parent) throws Exception {if (parent != null) {// 遍历子节点for (XNode child : parent.getChildren()) {// 如果是 package 标签,则扫描该包if ("package".equals(child.getName())) {// 获得包名String mapperPackage = child.getStringAttribute("name");// 添加到 configuration 中configuration.addMappers(mapperPackage);// 如果是 mapper 标签,} else {// 获得 resource、url、class 属性String resource = child.getStringAttribute("resource");String url = child.getStringAttribute("url");String mapperClass = child.getStringAttribute("class");// 使用相对于类路径的资源引用if (resource != null && url == null && mapperClass == null) {ErrorContext.instance().resource(resource);// 获得 resource 的 InputStream 对象InputStream inputStream = Resources.getResourceAsStream(resource);// 创建 XMLMapperBuilder 对象XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());// 执行解析mapperParser.parse();// 使用完全限定资源定位符(URL)} else if (resource == null && url != null && mapperClass == null) {ErrorContext.instance().resource(url);// 获得 url 的 InputStream 对象InputStream inputStream = Resources.getUrlAsStream(url);// 创建 XMLMapperBuilder 对象XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());// 执行解析mapperParser.parse();// 使用映射器接口实现类的完全限定类名} else if (resource == null && url == null && mapperClass != null) {// 获得 Mapper 接口Class<?> mapperInterface = Resources.classForName(mapperClass);// 添加到 configuration 中configuration.addMapper(mapperInterface);} else {throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");}}}}
}

到此,对xml配置文件的解析就结束了,回到开头的步骤2.中调用的重载build方法

/*** 创建 DefaultSqlSessionFactory 对象** @param config Configuration 对象* @return DefaultSqlSessionFactory 对象*/
public SqlSessionFactory build(Configuration config) {return new DefaultSqlSessionFactory(config); //构建者设计模式
}

2、执行sql流程

继续跟着上面的构建者设计模式把Configuration对象传到了SqlSessionFactory

我们拿到了SqlSessionFactory以后,调用openSession()方法。

SqlSession sqlSession = sqlSessionFactory.openSession();

我们先来简单介绍下SqlSession

SqlSession是一个接口,它有两个实现类:DefaultSqlSession(默认) 和 SqlSessionManager(弃用,不做介绍)

SqlSessionMyBatis中用于和数据库交互的顶层类,通常将它与ThreadLocal绑定,一个会话使用一个SqlSession,并且在使用完毕后需要close

public class DefaultSqlSession implements SqlSession {private final Configuration configuration;private final Executor executor;...
}

DefaultSqlSession中两个最重要的参数,Configuration与初始化的时候相同,Executor为执行器。

继续分析,初始化完毕后,我们就要执行sql语句了:

SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("com.riemann.mapper.UserMapper.getAllUser");

获得SqlSession

上面了解完SqlSession以及它的默认实现类DefaultSqlSession后,我们继续回到上面的sqlSessionFactory.openSession()分析。如下代码:

/*** 默认 SqlSessionFactory 实现类** @author Clinton Begin*/
public class DefaultSqlSessionFactory implements SqlSessionFactory {private final Configuration configuration;public DefaultSqlSessionFactory(Configuration configuration) {this.configuration = configuration;}//6. 进入openSession方法@Overridepublic SqlSession openSession() {//getDefaultExecutorType()传递的是SimpleExecutorreturn openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);}//7. 进入openSessionFromDataSource。//ExecutorType 为Executor的类型,TransactionIsolationLevel为事务隔离级别,autoCommit是否开启事务//openSession的多个重载方法可以指定获得的SqlSession的Executor类型和事务的处理private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {Transaction tx = null;try {// 获得 Environment 对象final Environment environment = configuration.getEnvironment();// 创建 Transaction 对象final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);// 创建 Executor 对象final Executor executor = configuration.newExecutor(tx, execType);// 创建 DefaultSqlSession 对象return new DefaultSqlSession(configuration, executor, autoCommit);} catch (Exception e) {// 如果发生异常,则关闭 Transaction 对象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中的api:

public class DefaultSqlSession implements SqlSession {...//8.进入selectList方法,多个重载方法@Overridepublic <E> List<E> selectList(String statement) {return this.selectList(statement, null);}@Overridepublic <E> List<E> selectList(String statement, Object parameter) {return this.selectList(statement, parameter, RowBounds.DEFAULT);}@Overridepublic <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {try {// 获得 MappedStatement 对象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();}}...
}

3、Executor执行器

Executor也是一个接口,它有三个常用的实现类:

  • BatchExecutor:重用语句并执行批量更新
  • ReuseExecutor:重用预处理语句PreparedStatements
  • SimpleExecutor:普通的执行器,默认

继续跟踪上面源码的步骤,到了executor.query()

/*** Executor 基类,提供骨架方法,从而使子类只要实现指定的几个抽象方法即可*/
public abstract class BaseExecutor implements Executor {...//此方法在SimpleExecutor的父类BaseExecutor中实现@Overridepublic <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {//根据传入的参数动态获得SQL语句,最后返回用BoundSql对象表示BoundSql boundSql = ms.getBoundSql(parameter);//为本次查询创建缓存的KeyCacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);// 查询return query(ms, parameter, rowBounds, resultHandler, key, boundSql);}@SuppressWarnings("unchecked")@Overridepublic <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());// 已经关闭,则抛出 ExecutorException 异常if (closed) {throw new ExecutorException("Executor was closed.");}// 清空本地缓存,如果 queryStack 为零,并且要求清空本地缓存。if (queryStack == 0 && ms.isFlushCacheRequired()) {clearLocalCache();}List<E> list;try {// queryStack + 1queryStack++;// 从一级缓存中,获取查询结果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 - 1queryStack--;}if (queryStack == 0) {// 执行延迟加载for (DeferredLoad deferredLoad : deferredLoads) {deferredLoad.load();}// issue #601// 清空 deferredLoadsdeferredLoads.clear();// 如果缓存级别是 LocalCacheScope.STATEMENT ,则进行清理if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {// issue #482clearLocalCache();}}return list;}...
}
/*** 简单的 Executor 实现类。** 1. 每次开始读或写操作,都创建对应的 Statement 对象。* 2. 执行完成后,关闭该 Statement 对象。*/
public class SimpleExecutor extends BaseExecutor {public SimpleExecutor(Configuration configuration, Transaction transaction) {super(configuration, transaction);}@Overridepublic <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {Statement stmt = null;try {Configuration configuration = ms.getConfiguration();// 传入参数创建StatementHanlder对象来执行查询StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);// 创建jdbc中的statement对象stmt = prepareStatement(handler, ms.getStatementLog());// 执行 StatementHandler  ,进行读操作return handler.query(stmt, resultHandler);} finally {// 关闭 StatementHandler 对象closeStatement(stmt);}}// 初始化 StatementHandler 对象private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {Statement stmt;// 获得 Connection 对象Connection connection = getConnection(statementLog);// 创建 Statement 或 PrepareStatement 对象stmt = handler.prepare(connection, transaction.getTimeout());// 设置 SQL 上的参数,例如 PrepareStatement 对象上的占位符handler.parameterize(stmt);return stmt;}}
// 获得 Connection 对象
protected Connection getConnection(Log statementLog) throws SQLException {// 获得 Connection 对象Connection connection = transaction.getConnection();// 如果 debug 日志级别,则创建 ConnectionLogger 对象,进行动态代理if (statementLog.isDebugEnabled()) {return ConnectionLogger.newInstance(connection, statementLog, queryStack);} else {return connection;}
}
@Override
public Connection getConnection() throws SQLException {// 连接为空,进行创建if (connection == null) {openConnection();}return connection;
}
/*** 获得 Connection 对象** @throws SQLException 获得失败*/
protected void openConnection() throws SQLException {if (log.isDebugEnabled()) {log.debug("Opening JDBC Connection");}// 获得连接connection = dataSource.getConnection();// 设置隔离级别if (level != null) {connection.setTransactionIsolation(level.getLevel());}// 设置 autoCommit 属性setDesiredAutoCommit(autoCommit);
}

上述的executor.query()方法几经转折,最后创建一个StatementHandler对象,然后将必要的参数传递给StatementHandler,使用StatementHandler来完成对数据库的查询,最终返回List结果集。

从上面的代码可以看出,Executor的功能和作用是:

  • 据传递参数,完成sql语句的动态解析,生成BoundSql对象,供StatementHandler使用;
  • 为查询创建缓存,以提高性能;
  • 创建JDBCStatement连接对象,传递给StatementHandler对象,返回List查询结果。

4、StatementHandler处理器

StatementHandler对象主要完成两个工种:

  • 对于JDBCPreparedStatement类型的对象,创建的过程中,我们使用的时sql语句字符串会包含若干个占位符,我们其后再对占位符进行设值。StatementHandler通过parameterize(statement)方法对Statement进行设值。
  • StatementHandler通过List query(Statement statement, ResultHandler resultHandler)方法来执行Statement,将和Statement对象返回resultSet封装成List

进入到StatementHandlerparameterize(statement) 方法的实现:

public class PreparedStatementHandler extends BaseStatementHandler {...@Overridepublic void parameterize(Statement statement) throws SQLException {//使用ParameterHandler对象来完成对Statement的设值parameterHandler.setParameters((PreparedStatement) statement);}...
}
/*** 默认 ParameterHandler 实现类*/
public class DefaultParameterHandler implements ParameterHandler {...@Overridepublic void setParameters(PreparedStatement ps) {ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());// 遍历 ParameterMapping 数组List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();if (parameterMappings != null) {for (int i = 0; i < parameterMappings.size(); i++) {// 获得 ParameterMapping 对象ParameterMapping parameterMapping = parameterMappings.get(i);if (parameterMapping.getMode() != ParameterMode.OUT) {// 获得值Object value;String propertyName = parameterMapping.getProperty();if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional paramsvalue = boundSql.getAdditionalParameter(propertyName);} else if (parameterObject == null) {value = null;} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {value = parameterObject;} else {MetaObject metaObject = configuration.newMetaObject(parameterObject);value = metaObject.getValue(propertyName);}// 获得 typeHandler、jdbcType 属性TypeHandler typeHandler = parameterMapping.getTypeHandler();JdbcType jdbcType = parameterMapping.getJdbcType();if (value == null && jdbcType == null) {jdbcType = configuration.getJdbcTypeForNull();}// 设置 ? 占位符的参数try {typeHandler.setParameter(ps, i + 1, value, jdbcType);} catch (TypeException | SQLException e) {throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);}}}}}...}

从上述代码可以看到StatementHandlerparameterize(statement) 方法调用了ParameterHandlersetParameters(preparedStatement) 方法。

该方法负责根据我们传入的参数,对statement对象的占位符进行赋值。

进入到StatementHandlerList query(Statement statement, ResultHandler resultHandler)

public class PreparedStatementHandler extends BaseStatementHandler {...@Overridepublic <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {// 1.调用preparedStatement.execute()方法,然后将resultSet交给ResultSetHandler处理PreparedStatement ps = (PreparedStatement) statement;// 2.执行查询ps.execute();// 3.使用ResultSetHandler来处理ResultSetreturn resultSetHandler.handleResultSets(ps);}...
}

从上述代码可以看出:StatementHandlerList query(Statement statement, ResultHandler resultHandler)方法实现,是调用了ResultSetHandlerhandleResultSets(preparedStatement)方法。该方法会将Statement语句执行后生成resultSet结果集转换成List结果集。

public class DefaultResultSetHandler implements ResultSetHandler {...@Overridepublic List<Object> handleResultSets(Statement stmt) throws SQLException {ErrorContext.instance().activity("handling results").object(mappedStatement.getId());// 多 ResultSet 的结果集合,每个 ResultSet 对应一个 Object 对象。而实际上,每个 Object 是 List<Object> 对象。// 在不考虑存储过程的多 ResultSet 的情况,普通的查询,实际就一个 ResultSet ,也就是说,multipleResults 最多就一个元素。final List<Object> multipleResults = new ArrayList<>();int resultSetCount = 0;// 获得首个 ResultSet 对象,并封装成 ResultSetWrapper 对象ResultSetWrapper rsw = getFirstResultSet(stmt);// 获得 ResultMap 数组// 在不考虑存储过程的多 ResultSet 的情况,普通的查询,实际就一个 ResultSet ,也就是说,resultMaps 就一个元素。List<ResultMap> resultMaps = mappedStatement.getResultMaps();int resultMapCount = resultMaps.size();validateResultMapsCount(rsw, resultMapCount); // 校验while (rsw != null && resultMapCount > resultSetCount) {// 获得 ResultMap 对象ResultMap resultMap = resultMaps.get(resultSetCount);// 处理 ResultSet ,将结果添加到 multipleResults 中handleResultSet(rsw, resultMap, multipleResults, null);// 获得下一个 ResultSet 对象,并封装成 ResultSetWrapper 对象rsw = getNextResultSet(stmt);// 清理cleanUpAfterHandlingResultSet();// resultSetCount ++resultSetCount++;}// 因为 `mappedStatement.resultSets` 只在存储过程中使用,本系列暂时不考虑,忽略即可String[] resultSets = mappedStatement.getResultSets();if (resultSets != null) {while (rsw != null && resultSetCount < resultSets.length) {ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);if (parentMapping != null) {String nestedResultMapId = parentMapping.getNestedResultMapId();ResultMap resultMap = configuration.getResultMap(nestedResultMapId);handleResultSet(rsw, resultMap, null, parentMapping);}rsw = getNextResultSet(stmt);cleanUpAfterHandlingResultSet();resultSetCount++;}}// 如果是 multipleResults 单元素,则取首元素返回return collapseSingleResultList(multipleResults);}...
}

OK,至此传统方式源码分析到此结束,我们继续来分析Mapper代理方式的源码。

二、Mapper代理方式源码分析

分析之前,我们来回顾下写法:

/*** mapper代理方式*/
public void test2() throws IOException {InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = factory.openSession();// 使用JDK动态代理对mapper接口产生代理对象UserMapper mapper = sqlSession.getMapper(UserMapper.class);//代理对象调用接口中的任意方法,执行的都是动态代理中的invoke方法List<User> allUser = mapper.findAllUser();}

思考一个问题:通常Mapper接口我们都没有实现的方法却可以使用,是为什么呢?答案很简单:动态代理。

开始之前介绍一下MyBatis初始化时对接口的处理:MapperRegistryConfiguration中的一个属性,它内部维护了一个HashMap,用于存放mapper接口的工厂类,每个接口对应一个工厂。

/*** MapperRegistry 对象*/
protected final MapperRegistry mapperRegistry = new MapperRegistry(this);

mappers中可以配置接口的包路径,或者某个接口的具体类。

<mappers><!--普通写法--><!--<mapper resource="OrderMapper.xml" />--><!--注解写法--><!--<mapper class="com.riemann.mapper.UserMapper" />--><!--上面那个方法如果mapper很多的话,要些很多个,所以这里推荐包扫描--><!--这里要注意!!!要在resources里建与上面同包同名 这种形式建立目录:com/riemann/mapper--><package name="com.riemann.mapper"/>
</mappers>

当解析mappers标签时,它会判断解析到的是mapper配置文件时,会再将对应配置文件中的增删改查标签一一封装成MappedStatement对象,存入mappedStatements中。当判断解析到接口时,会建此接口对应的MappedProxyFactory对象,存入HashMap中,key=接口的字节码对象,value=此接口对应的MappedProxyFactory对象

1、getMapper分析

// DefaultSqlSession的getMapper
public <T> T getMapper(Class<T> type) {return configuration.getMapper(type, this);
}// Configuration的getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {return mapperRegistry.getMapper(type, sqlSession);
}// MapperRegistry的getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {// 获得 MapperProxyFactory 对象final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);// 不存在,则抛出 BindingException 异常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);}
}
protected T newInstance(MapperProxy<T> mapperProxy) {return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
}//MapperProxyFactory类中的newInstance方法
public T newInstance(SqlSession sqlSession) {// 创建了JDK动态代理的invocationHandler接口的实现类mapperProxyfinal MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);// 调用了重载方法return newInstance(mapperProxy);
}
public class MapperProxy<T> implements InvocationHandler, Serializable {/*** SqlSession 对象*/private final SqlSession sqlSession;/*** Mapper 接口*/private final Class<T> mapperInterface;/*** 方法与 MapperMethod 的映射** 从 {@link MapperProxyFactory#methodCache} 传递过来*/private final Map<Method, MapperMethod> methodCache;// 构造,传入了SqlSession,说明每个session中的代理对象的不同的!public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {this.sqlSession = sqlSession;this.mapperInterface = mapperInterface;this.methodCache = methodCache;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {// 如果是 Object 定义的方法,直接调用if (Object.class.equals(method.getDeclaringClass())) {return method.invoke(this, args);} else if (isDefaultMethod(method)) {return invokeDefaultMethod(proxy, method, args);}} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable(t);}// 获得 MapperMethod 对象final MapperMethod mapperMethod = cachedMapperMethod(method);// 重点在这:MapperMethod最终调用了执行的方法return mapperMethod.execute(sqlSession, args);}...
}

2、invoke()分析

在动态代理返回了示例后,我们就可以直接调用mapper类中的方法了,但代理对象调用方法,执行是在MapperProxyinvoke方法中。

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {// 如果是 Object 定义的方法,直接调用if (Object.class.equals(method.getDeclaringClass())) {return method.invoke(this, args);} else if (isDefaultMethod(method)) {return invokeDefaultMethod(proxy, method, args);}} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable(t);}// 获得 MapperMethod 对象final MapperMethod mapperMethod = cachedMapperMethod(method);// 重点在这:MapperMethod最终调用了执行的方法return mapperMethod.execute(sqlSession, args);
}

进入execute方法

public Object execute(SqlSession sqlSession, Object[] args) {Object result;//判断mapper中的方法类型,最终调用的还是SqlSession中的方法switch (command.getType()) {case INSERT: {// 转换参数Object param = method.convertArgsToSqlCommandParam(args);// 执行 INSERT 操作// 转换 rowCountresult = rowCountResult(sqlSession.insert(command.getName(), param));break;}case UPDATE: {// 转换参数Object param = method.convertArgsToSqlCommandParam(args);// 转换 rowCountresult = rowCountResult(sqlSession.update(command.getName(), param));break;}case DELETE: {// 转换参数Object param = method.convertArgsToSqlCommandParam(args);// 转换 rowCountresult = rowCountResult(sqlSession.delete(command.getName(), param));break;}case SELECT:// 无返回,并且有 ResultHandler 方法参数,则将查询的结果,提交给 ResultHandler 进行处理if (method.returnsVoid() && method.hasResultHandler()) {executeWithResultHandler(sqlSession, args);result = null;// 执行查询,返回列表} else if (method.returnsMany()) {result = executeForMany(sqlSession, args);// 执行查询,返回 Map} else if (method.returnsMap()) {result = executeForMap(sqlSession, args);// 执行查询,返回 Cursor} else if (method.returnsCursor()) {result = executeForCursor(sqlSession, args);// 执行查询,返回单个对象} else {// 转换参数Object param = method.convertArgsToSqlCommandParam(args);// 查询单条result = sqlSession.selectOne(command.getName(), param);if (method.returnsOptional() &&(result == null || !method.getReturnType().equals(result.getClass()))) {result = Optional.ofNullable(result);}}break;case FLUSH:result = sqlSession.flushStatements();break;default:throw new BindingException("Unknown execution method for: " + command.getName());}// 返回结果为 null ,并且返回类型为基本类型,则抛出 BindingException 异常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;
}

OK,至此Mapper代理方式源码分析到此结。

三、MyBatis源码中涉及到的设计模式

mybatis源码中至少用到了以下的设计模式的使用:

模式 mybatis体现
Builder模式 例如SqlSessionFactoryBuilder、Environment
工厂方法模式 例如SqlSessionFactory、TransactionFactory、LogFactory
单例模式 例如ErrorContext、LogFactory
代理模式 MyBatis实现的核心,例如MapperProxy、ConnectionLogger,用的jdk的动态代理还有executor.loader包使用了cglib或者javassist达到延迟加载的效果。
组合模式 例如SqlNode和各个子类ChooseSqlNode
模板方法模式 例如BaseExecutor、SimpleExecutor
适配器模式 例如Log的MyBatis接口和它对jdbc、log4j等各种日志框架的适配实现
装饰者模式 例如Cache包中的cache.decorators子包中等各个装饰者的实现
迭代器模式 例如PropertyTokenizer

我的架构梦:(三)MyBatis源码分析相关推荐

  1. MyBatis源码分析(一)MyBatis整体架构分析

    文章目录 系列文章索引 一.为什么要用MyBatis 1.原始JDBC的痛点 2.Hibernate 和 JPA 3.MyBatis的特点 4.MyBatis整体架构 5.MyBatis主要组件及其相 ...

  2. 十年老架构师神级推荐,MyBatis源码分析,再也不用为源码担忧了

    十年老架构师神级推荐,MyBatis源码分析,再也不用为源码担忧了 前言 MyBatis是一个优秀的持久层ORM框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL 本身,而不需要花 ...

  3. springboot集成mybatis源码分析-mybatis的mapper执行查询时的流程(三)

    springboot集成mybatis源码分析-mybatis的mapper执行查询时的流程(三) 例: package com.example.demo.service;import com.exa ...

  4. 【Qt】Log4Qt(三)源码分析

    Log4Qt(三)源码分析 1.分层架构 1.1 核心对象 1.2 支持对象 2.源码分析 2.1 宏 2.1.1 LOG4QT_DECLARE_QCLASS_LOGGER 2.1.2 LOG4QT_ ...

  5. MyBatis 源码分析 - 缓存原理

    1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 Redis 或 memcached 等缓存中间件,拦截大量奔向数据库的请求,减轻数据库压力.作为一个重要的组件,MyBatis 自然 ...

  6. MyBatis 源码分析 - 内置数据源

    1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...

  7. MyBatis 源码分析 - SQL 的执行过程

    本文速览 本篇文章较为详细的介绍了 MyBatis 执行 SQL 的过程.该过程本身比较复杂,牵涉到的技术点比较多.包括但不限于 Mapper 接口代理类的生成.接口方法的解析.SQL 语句的解析.运 ...

  8. MyBatis 源码分析 - 映射文件解析过程

    1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...

  9. MyBatis 源码分析系列文章导读

    1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ...

  10. Mybatis 源码分析(一)配置文件加载流程

    Mybatis 源码分析(一)配置文件加载流程 1.项目构建 引入依赖 <dependency><groupId>org.mybatis</groupId>< ...

最新文章

  1. python怎么将输入的数字变成列表_Python键盘输入转换为列表的实例
  2. 剑指offer 最小的k个数
  3. mysql导出bacpac_在 Azure 中备份应用
  4. 牛客挑战赛47 D Lots of Edges(最短路+递归枚举子集)
  5. java本地创建zk节点
  6. AD DS 域控与成员计算机的时间一致性
  7. 滑动窗口协议(GBN, SR)
  8. 路飞学城Python-Day5
  9. 腾讯校招都会问些什么?| 五面腾讯(Java岗)经历分享
  10. 计算机软件的卸载,电脑里那些软件是不需要的?应该怎么卸载?
  11. 引用第三方SDK产生依赖冲突
  12. 视频原声能去掉吗?怎么批量处理
  13. 动图如何在线制作?教你一键在线制作动图
  14. 可可直播电视---开通官网
  15. Python的基本语法(十一)(持续更新)
  16. 爬了3000万QQ用户数据,挖出了花千骨赵丽颖的QQ号
  17. 不知名的有DIO、SCK、RCK引脚的四位LED数码管模块
  18. c++程序设计基础(第五版)(上) 习题与解答
  19. 计算机新课标学习心得体会,【精品】新课标学习心得体会模板锦集10篇
  20. python数据分析与挖掘实战(商品零售购物篮分析)

热门文章

  1. 如何配置ODBC数据源
  2. 快消供应链管理:建立新零售生态系统的基本准则及数字化渠道管理
  3. excel筛选大于某个值数据并用颜色标注
  4. 在springboot中使用JWT自定义生成Token信息,接口请求时校验Token(在Shiro基础上)
  5. 嵌入式Linux视频笔记----Linux基础入门
  6. 技术粗糙,效果不错:广告主眼中的微博商业化-微博粉丝通广告试用手记
  7. Linux----知识储备
  8. iphone开发资源汇总(二)
  9. ASP.NET中/应用程序中的服务器错误的方法
  10. 时间复杂度的五个记号