Mybatis工作流程及其原理与解析

Mybatis简介:

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。本文将通过debug的方式来了解其工作原理。

Mybatis核心类:

SqlSessionFactory:每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或通过Java的方式构建出 SqlSessionFactory 的实例。SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,建议使用单例模式或者静态单例模式。一个SqlSessionFactory对应配置文件中的一个环境(environment),如果你要使用多个数据库就配置多个环境分别对应一个SqlSessionFactory。

SqlSession:SqlSession是一个接口,它有2个实现类,分别是DefaultSqlSession(默认使用)以及SqlSessionManager。SqlSession通过内部存放的执行器(Executor)来对数据进行CRUD。此外SqlSession不是线程安全的,因为每一次操作完数据库后都要调用close对其进行关闭,官方建议通过try-finally来保证总是关闭SqlSession。

Executor:Executor(执行器)接口有两个实现类,其中BaseExecutor有三个继承类分别是BatchExecutor(重用语句并执行批量更新),ReuseExecutor(重用预处理语句prepared statement,跟Simple的唯一区别就是内部缓存statement),SimpleExecutor(默认,每次都会创建新的statement)。以上三个就是主要的Executor。通过下图可以看到Mybatis在Executor的设计上面使用了装饰器模式,我们可以用CachingExecutor来装饰前面的三个执行器目的就是用来实现缓存。

MappedStatement:MappedStatement就是用来存放我们SQL映射文件中的信息包括sql语句,输入参数,输出参数等等。一个SQL节点对应一个MappedStatement对象。

Mybatis工作流程:

下面将通过debug方式对Mybatis进行一步步解析。首先贴出我的mybatis-config.xml文件以及Mapper.xml文件。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="jdbc.properties"></properties>
  7. <settings>
  8. <setting name="cacheEnabled" value="true"/>
  9. <setting name="defaultExecutorType" value="REUSE"/>
  10. </settings>
  11. <typeAliases>
  12. <typeAlias alias="User" type="com.ctc.model.User"/>
  13. </typeAliases>
  14. <environments default="development">
  15. <environment id="development">
  16. <transactionManager type="JDBC"/>
  17. <dataSource type="POOLED">
  18. <property name="driver" value="${driver}"/>
  19. <property name="url" value="${url}"/>
  20. <property name="username" value="${username}"/>
  21. <property name="password" value="${password}"/>
  22. </dataSource>
  23. </environment>
  24. </environments>
  25. <mappers>
  26. <package name="com.ctc.mapper"/>
  27. </mappers>
  28. </configuration>
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.ctc.mapper.UserMapper">
  6. <cache readOnly="true" size="200" eviction="FIFO"></cache>
  7. <sql id="select"> select * from user </sql>
  8. <select id="selectUser" resultMap="selectUserMap" useCache="true">
  9. <include refid="select"></include> User where id = #{id}
  10. </select>
  11. <resultMap type="User" id="selectUserMap">
  12. <result property="name" column="username"/>
  13. </resultMap>
  14. <insert id="insertUser" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
  15. insert into User (username,birthday,sex,address)
  16. values (#{name},#{birthday},#{sex},#{address})
  17. </insert>
  18. <update id="updateUser">
  19. update User set username = #{username},birthday = #{birthday},
  20. sex = #{sex},address = #{address} where id = #{id}
  21. </update>
  22. <delete id="deleteUser" >
  23. delete from User where id = #{id}
  24. </delete>
  25. <select id="selectUserByName" resultMap="selectUserMap">
  26. select * from User where sex = #{param1}
  27. <choose>
  28. <when test="{param2} != null">
  29. and username like #{param2}
  30. </when>
  31. <otherwise>and address = #{parma3}</otherwise>
  32. </choose>
  33. </select>
  34. <select id="selectUserCount" resultType="int" >
  35. select count(*) from user where username like #{username}
  36. </select>
  37. <select id="selectUserNew" resultMap="selectUserMap">
  38. <bind name="pattern" value="'%' + name + '%'" />
  39. <include refid="select"></include>
  40. <where>
  41. <if test="pattern !=null">
  42. username like #{pattern}
  43. </if>
  44. <if test="sex !=null">
  45. and sex = #{sex}
  46. </if>
  47. <if test="address != null">
  48. and address = #{address}
  49. </if>
  50. </where>
  51. </select>
  52. <select id="selectUserByIds" resultMap="selectUserMap">
  53. <include refid="select"></include>
  54. where id in
  55. <foreach collection="list" item="id" index="0" open="(" close=")" separator="," >
  56. #{id}
  57. </foreach>
  58. </select>
  59. </mapper>

 

第一步通过SqlSessionFactoryBuilder创建SqlSessionFactory:

    首先在SqlSessionFactoryBuilder的build()方法中可以看到MyBatis内部定义了一个类XMLConfigBuilder用来解析配置文件mybatis-config.xml。针对配置文件中的每一个节点进行解析并将数据存放到Configuration这个对象中,紧接着使用带有Configuration的构造方法发返回一个DefautSqlSessionFactory。

  1. public SqlSessionFactory build(InputStream inputStream) {
  2. return build(inputStream, null, null);
  3. }
  4. public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
  5. try {
  6. XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
  7. //解析mybatis-config.xml
  8.  return build(parser.parse());
  9. } catch (Exception e) {
  10. throw ExceptionFactory.wrapException("Error building SqlSession.", e);
  11. } finally {
  12. ErrorContext.instance().reset();
  13. try {
  14. inputStream.close();
  15. } catch (IOException e) {
  16. // Intentionally ignore. Prefer previous error.
  17. }
  18. }
  19. }
  20. //返回SqlSessionFactory,默认使用的是实现类DefaultSqlSessionFactory
  21. public SqlSessionFactory build(Configuration config) {
  22. return new DefaultSqlSessionFactory(config);
  23. }
  24.  public Configuration parse() {
  25. if (parsed) {
  26. throw new BuilderException("Each XMLConfigBuilder can only be used once.");
  27. }
  28. parsed = true;
  29. //获取根节点configuration
  30.  parseConfiguration(parser.evalNode("/configuration"));
  31. return configuration;
  32. }
  33. //开始解析mybatis-config.xml,并把解析后的数据存放到configuration中
  34. private void parseConfiguration(XNode root) {
  35. try {
  36. //保存mybatis-config.xml中的标签setting,本例中开启全局缓存cacheEnabled,设置默认执行器defaultExecutorType=REUSE
  37.  Properties settings = settingsAsPropertiess(root.evalNode("settings"));
  38. //issue #117 read properties first
  39. //解析是否配置了外部properties,例如本例中配置的jdbc.propertis
  40.  propertiesElement(root.evalNode("properties"));
  41. //查看是否配置了VFS,默认没有,本例也没有使用
  42.  loadCustomVfs(settings);
  43. //查看是否用了类型别名,减少完全限定名的冗余,本例中使用了别名User代替了com.ctc.Model.User
  44.  typeAliasesElement(root.evalNode("typeAliases"));
  45. //查看是否配置插件来拦截映射语句的执行,例如拦截Executor的Update方法,本例没有使用
  46.  pluginElement(root.evalNode("plugins"))
  47. //查看是否配置了ObjectFactory,默认情况下使用对象的无参构造方法或者是带有参数的构造方法,本例没有使用
  48.  objectFactoryElement(root.evalNode("objectFactory"));
  49. //查看是否配置了objectWrapperFatory,这个用来或者ObjectWapper,可以访问:对象,Collection,Map属性。本例没有使用
  50. objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
  51. //查看是否配置了reflectorFactory,mybatis的反射工具,提供了很多反射方法。本例没有使用
  52.  reflectorFactoryElement(root.evalNode("reflectorFactory"));
  53. //放入参数到configuration对象中
  54.  settingsElement(settings);
  55. // read it after objectFactory and objectWrapperFactory issue #631
  56. //查看数据库环境配置
  57.  environmentsElement(root.evalNode("environments"));
  58. //查看是否使用多种数据库,本例没有使用
  59.  databaseIdProviderElement(root.evalNode("databaseIdProvider"));
  60. //查看是否配置了新的类型处理器,如果跟处理的类型跟默认的一致就会覆盖。本例没有使用
  61.  typeHandlerElement(root.evalNode("typeHandlers"));
  62. //查看是否配置SQL映射文件,有四种配置方式,resource,url,class以及自动扫包package。本例使用package
  63.  mapperElement(root.evalNode("mappers"));
  64. } catch (Exception e) {
  65. throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
  66. }
  67. }

第二步通过SqlSessionFactory创建SqlSession:

  1.  @Override
  2. public SqlSession openSession() {
  3. return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  4. }
  5. private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  6. Transaction tx = null;
  7. try {
  8. //拿到前文从mybatis中解析到的数据库环境配置
  9.  final Environment environment = configuration.getEnvironment();
  10. final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
  11. //拿到jdbc的事务管理器,有两种一种是jbc,一种的managed。本例使用的是JdbcTransaction
  12.  tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
  13. //从mybatis配置文件可以看到本例使用了REUSE,因此返回的是ReuseExecutor并把事务传入对象中
  14.  final Executor executor = configuration.newExecutor(tx, execType);
  15. return new DefaultSqlSession(configuration, executor, autoCommit);
  16. } catch (Exception e) {
  17. closeTransaction(tx); // may have fetched a connection so lets call close()
  18. throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
  19. } finally {
  20. ErrorContext.instance().reset();
  21. }
  22. }
  23. public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
  24. executorType = executorType == null ? defaultExecutorType : executorType;
  25. executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  26. Executor executor;
  27. if (ExecutorType.BATCH == executorType) {
  28. executor = new BatchExecutor(this, transaction);
  29. } else if (ExecutorType.REUSE == executorType) {
  30. executor = new ReuseExecutor(this, transaction);
  31. } else {
  32. executor = new SimpleExecutor(this, transaction);
  33. }
  34. if (cacheEnabled) {
  35. executor = new CachingExecutor(executor);
  36. }
  37. executor = (Executor) interceptorChain.pluginAll(executor);
  38. return executor;
  39. }
  40. //返回一个SqlSession,默认使用DefaultSqlSession
  41. public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  42. this.configuration = configuration;
  43. this.executor = executor;
  44. this.dirty = false;
  45. this.autoCommit = autoCommit;
  46. }

第三步通过SqlSession拿到Mapper对象的代理:

  1. @Override
  2. public <T> T getMapper(Class<T> type) {
  3. return configuration.<T>getMapper(type, this);
  4. }
  5. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  6. //前文解析Mybatis-config.xml的时候,在解析标签mapper就是用configuration对象的mapperRegistry存放数据
  7.  return mapperRegistry.getMapper(type, sqlSession);
  8. }
  9.   @SuppressWarnings("unchecked")
  10.   public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  11. //knownMapper是一个HashMap在存放mapperRegistry的过程中,以每个Mapper对象的类型为Key, MapperProxyFactory 为value保存。
  12. //例如本例中保存的就是Key:com.ctc.mapper.UserMapper,value就是保存了key的MapperProxyFactory对象
  13. final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
  14.     if (mapperProxyFactory == null) {
  15.       throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
  16.     }
  17.     try {
  18.       return mapperProxyFactory.newInstance(sqlSession);
  19.     } catch (Exception e) {
  20.       throw new BindingException("Error getting mapper instance. Cause: " + e, e);
  21.     }
  22.   }
  23. public T newInstance(SqlSession sqlSession) {
  24. //生成一个mapperProxy对象,这个对象实现了InvocationHandler, Serializable。就是JDK动态代理中的方法调用处理器
  25.  final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
  26. return newInstance(mapperProxy);
  27. }
  28. public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
  29. this.sqlSession = sqlSession;
  30. this.mapperInterface = mapperInterface;
  31. this.methodCache = methodCache;
  32. }
  33. @SuppressWarnings("unchecked")
  34. protected T newInstance(MapperProxy<T> mapperProxy) {
  35. //通过JDK动态代理生成一个Mapper的代理,在本例中的就是UserMapper的代理类,它实现了UserMapper接口
  36.  return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  37. }

第四步通过MapperProxy调用Maper中相应的方法:

  1. @Override
  2. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  3. //判断当前调用的method是不是Object中声明的方法,如果是的话直接执行。
  4.  if (Object.class.equals(method.getDeclaringClass())) {
  5. try {
  6. return method.invoke(this, args);
  7. } catch (Throwable t) {
  8. throw ExceptionUtil.unwrapThrowable(t);
  9. }
  10. }
  11. final MapperMethod mapperMethod = cachedMapperMethod(method);
  12. return mapperMethod.execute(sqlSession, args);
  13. }
  14. //把当前请求放入一个HashMap中,一旦下次还是同样的方法进来直接返回。
  15. private MapperMethod cachedMapperMethod(Method method) {
  16. MapperMethod mapperMethod = methodCache.get(method);
  17. if (mapperMethod == null) {
  18. mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
  19. methodCache.put(method, mapperMethod);
  20. }
  21. return mapperMethod;
  22. }
  23.   public Object execute(SqlSession sqlSession, Object[] args) {
  24.     Object result;
  25.     switch (command.getType()) {
  26.       case INSERT: {
  27.         Object param = method.convertArgsToSqlCommandParam(args);
  28.         result = rowCountResult(sqlSession.insert(command.getName(), param));
  29.         break;
  30.       }
  31.       case UPDATE: {
  32.         Object param = method.convertArgsToSqlCommandParam(args);
  33.         result = rowCountResult(sqlSession.update(command.getName(), param));
  34.         break;
  35.       }
  36.       case DELETE: {
  37.         Object param = method.convertArgsToSqlCommandParam(args);
  38.         result = rowCountResult(sqlSession.delete(command.getName(), param));
  39.         break;
  40.       }
  41.       case SELECT:
  42.         if (method.returnsVoid() && method.hasResultHandler()) {
  43.           executeWithResultHandler(sqlSession, args);
  44.           result = null;
  45.         } else if (method.returnsMany()) {
  46.           result = executeForMany(sqlSession, args);
  47.         } else if (method.returnsMap()) {
  48.           result = executeForMap(sqlSession, args);
  49.         } else if (method.returnsCursor()) {
  50.           result = executeForCursor(sqlSession, args);
  51.         } else {
  52. //本次案例会执行selectOne
  53.           Object param = method.convertArgsToSqlCommandParam(args);
  54.           result = sqlSession.selectOne(command.getName(), param);
  55.         }
  56.         break;
  57.       case FLUSH:
  58.         result = sqlSession.flushStatements();
  59.         break;
  60.       default:
  61.         throw new BindingException("Unknown execution method for: " + command.getName());
  62.     }
  63.     if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
  64.       throw new BindingException("Mapper method '" + command.getName()
  65.           + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
  66.     }
  67.     return result;
  68.   }
  69.   @Override
  70.   public <T> T selectOne(String statement, Object parameter) {
  71.     // Popular vote was to return null on 0 results and throw exception on too many.
  72.     List<T> list = this.<T>selectList(statement, parameter);
  73.     if (list.size() == 1) {
  74.       return list.get(0);
  75.     } else if (list.size() > 1) {
  76.       throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
  77.     } else {
  78.       return null;
  79.     }
  80.   }
  81.   @Override
  82.   public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  83.     try {
  84.       MappedStatement ms = configuration.getMappedStatement(statement);
  85.       return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
  86.     } catch (Exception e) {
  87.       throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
  88.     } finally {
  89.       ErrorContext.instance().reset();
  90.     }
  91.   }
  92. //这边调用的是CachingExecutor类的query,还记得前文解析mybatis-config.xml的时候我们指定了REUSE但是因为在配置文件中开启了缓存
  93. //所以ReuseExecutor被CachingExecotur装饰,新增了缓存的判断,最后还是会调用ReuseExecutor
  94.   @Override
  95.   public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
  96.     BoundSql boundSql = ms.getBoundSql(parameterObject);
  97.     CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
  98.     return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  99.   }
  100.   @Override
  101.   public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
  102.       throws SQLException {
  103.     Cache cache = ms.getCache();
  104.     if (cache != null) {
  105.       flushCacheIfRequired(ms);
  106.       if (ms.isUseCache() && resultHandler == null) {
  107.         ensureNoOutParams(ms, parameterObject, boundSql);
  108.         @SuppressWarnings("unchecked")
  109.         List<E> list = (List<E>) tcm.getObject(cache, key);
  110.         if (list == null) {
  111. //如果缓存中没有数据则查询数据库
  112.           list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  113. //结果集放入缓存
  114.           tcm.putObject(cache, key, list); // issue #578 and #116
  115.         }
  116.         return list;
  117.       }
  118.     }
  119.     return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  120.   }

 

 

 

 

 

(0)

相关推荐