MyBatisMyBatis框架及原理分析

MyBatis 是支持定制化
SQL、存储过程和高档映射的美妙之持久层框架,其重要性就是好2件业务:

  1. 封装JDBC操作
  2. 以反射打通Java类与SQL语句之间的彼此转换

MyBatis的要害设计目的就是是深受咱们本着实行SQL语句时对输入输出的数目管理进一步惠及,所以方便地刻画来SQL和造福地赢得SQL的施行结果才是MyBatis的中坚竞争力。

 

MyBatis的配置

MyBatis框架和任何绝大部分框架一样,需要一个配置文件,其配备文件约如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="false"/>
        <!--<setting name="logImpl" value="STDOUT_LOGGING"/> &lt;!&ndash; 打印日志信息 &ndash;&gt;-->
    </settings>

    <typeAliases>
        <typeAlias type="com.luo.dao.UserDao" alias="User"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/> <!--事务管理类型-->
            <dataSource type="POOLED">
                <property name="username" value="luoxn28"/>
                <property name="password" value="123456"/>
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.1.150/ssh_study"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="userMapper.xml"/>
    </mappers>

</configuration>

以上配置中,最要紧之是数据库参数的布,比如用户名密码等,如果安排了数据表对应之mapper文件,则要拿其参加到<mappers>节点下。 

 

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维护一漫漫<select|update|delete|insert>节点的卷入
  • SqlSource            
     负责根据用户传递的parameterObject,动态地生成SQL语句,将信息打包到BoundSql对象中,并回
  • BoundSql              表示动态变化的SQL语句以及对应的参数信息

以上重大成员以平等不好数据库操作中基本都见面提到,在SQL操作中要用关爱之凡SQL参数什么时候给装以及结果集怎么变换为JavaBean对象的,这片独过程恰好对应StatementHandler和ResultSetHandler类中的拍卖逻辑。

MyBatis 1

(图片来自[《深入理解mybatis原理》\ MyBatis的架构设计以及实例分析](http://blog.csdn.net/luanlouis/article/details/40422941))

 

MyBatis的初始化

MyBatis的初始化的经过实际上就是分析配置文件及初始化Configuration的过程,MyBatis的初始化过程可用以下几执代码来发挥:

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.
        }
    }
}

XMLConfigBuilder对象见面进行XML配置文件之辨析,实际为configuration节点的解析操作。

// XMLConfigBuilder类
public Configuration parse() {
    if (parsed) {
        throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
}

private void parseConfiguration(XNode root) {
    try {
        //issue #117 read properties first
        propertiesElement(root.evalNode("properties"));
        Properties settings = settingsAsProperties(root.evalNode("settings"));
        loadCustomVfs(settings);
        typeAliasesElement(root.evalNode("typeAliases"));
        pluginElement(root.evalNode("plugins"));
        objectFactoryElement(root.evalNode("objectFactory"));
        objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        reflectorFactoryElement(root.evalNode("reflectorFactory"));
        settingsElement(settings);
        // read it after objectFactory and objectWrapperFactory issue #631

        /* 处理environments节点数据 */
        environmentsElement(root.evalNode("environments"));
        databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        typeHandlerElement(root.evalNode("typeHandlers"));
        mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
}

于configuration节点下会依次解析properties/settings/…/mappers等节点配置。在解析environments节点时,会因transactionManager的配备来创造事务管理器,根据dataSource的布来创造DataSource对象,这间含了数据库登录的有关消息。在解析mappers节点时,会念博该节点下具有的mapper文件,然后进行辨析,并将分析后底结果存到configuration对象吃。

// XMLConfigBuilder类
private void environmentsElement(XNode context) throws Exception {
    if (context != null) {
        if (environment == null) {
            environment = context.getStringAttribute("default");
        }
        for (XNode child : context.getChildren()) {
            String id = child.getStringAttribute("id");
            if (isSpecifiedEnvironment(id)) {

                /* 创建事务管理器 */
                TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                DataSource dataSource = dsFactory.getDataSource();

                /* 建造者模式 设计模式 */
                Environment.Builder environmentBuilder = new Environment.Builder(id)
                        .transactionFactory(txFactory)
                        .dataSource(dataSource);
                configuration.setEnvironment(environmentBuilder.build());
            }
        }
    }
}

// 解析单独的mapper文件
private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        if ("package".equals(child.getName())) {
          String mapperPackage = child.getStringAttribute("name");
          configuration.addMappers(mapperPackage);
        } else {
          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);
            InputStream inputStream = Resources.getResourceAsStream(resource);
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
            mapperParser.parse(); // 开始解析mapper文件了 :)
          } else if (resource == null && url != null && mapperClass == null) {
            ErrorContext.instance().resource(url);
            InputStream inputStream = Resources.getUrlAsStream(url);
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
            mapperParser.parse();
          } else if (resource == null && url == null && mapperClass != null) {
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
          } else {
            throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
          }
        }
      }
    }
  }

分析了MyBatis配置文件后,configuration就初始化完成了,然后因configuration对象来创造SqlSession,到此时,MyBatis的初始化的征程已经走了事了。

// SqlSessionFactoryBuilder类
public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
}

 

MyBatis的SQL查询流程

SQL语句的实行才是MyBatis的重要职责,该过程尽管通过封装JDBC进行操作,然后使Java反射技术成功JavaBean对象及数据库参数之间的并行转换,这种映射关系虽是发生TypeHandler对象来完成的,在获取数据表对应的首批数据时,会保留该表所有列的数据库类型,大致逻辑如下所示:

/* Get resultSet metadata */
ResultSetMetaData metaData = resultSet.getMetaData();
int column = metaData.getColumnCount();

for (int i = 1; i <= column; i++) {
    JdbcType jdbcType = JdbcType.forCode(metaData.getColumnType(i));
    typeHandlers.add(TypeHandlerRegistry.getTypeHandler(jdbcType));

    columnNames.add(metaData.getColumnName(i));
}

 

以如下代码进行SQL查询操作:

sqlSession = sessionFactory.openSession();

User user = sqlSession.selectOne("com.luo.dao.UserDao.getUserById", 1);
System.out.println(user);

开创sqlSession的历程实际上就算是冲configuration中的布局来创造对应之近乎,然后返回创建的sqlSession对象。调用selectOne方法开展SQL查询,selectOne方法最后调用的凡selectList,在selectList中,会询问configuration中蕴藏的MappedStatement对象,mapper文件中一个sql语句之部署相应一个MappedStatement对象,然后调用执行器进行查询操作。

// DefaultSqlSession类
public <T> T selectOne(String statement, Object parameter) {
    // Popular vote was to return null on 0 results and throw exception on too many.
    List<T> list = this.<T>selectList(statement, parameter);
    if (list.size() == 1) {
        return list.get(0);
    } else if (list.size() > 1) {
        throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
    } else {
        return null;
    }
}

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

执行器在query操作中,优先会询问缓存是否中,命中则直接回,否则由数据库中询问。

// CachingExecutor类
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    /* 获取关联参数的sql,boundSql */
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    /* 创建cache key值 */
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
      throws SQLException {
    /* 获取二级缓存实例 */
    Cache cache = ms.getCache();
    if (cache != null) {
        flushCacheIfRequired(ms);
        if (ms.isUseCache() && resultHandler == null) {
            ensureNoOutParams(ms, parameterObject, boundSql);
            @SuppressWarnings("unchecked")
            List<E> list = (List<E>) tcm.getObject(cache, key);
            if (list == null) {
                list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
                tcm.putObject(cache, key, list); // issue #578 and #116
            }
            return list;
        }
    }
    return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    /**
     * 先往localCache中插入一个占位对象,这个地方
     */
    localCache.putObject(key, EXECUTION_PLACEHOLDER);
    try {
        list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
        localCache.removeObject(key);
    }

    /* 往缓存中写入数据,也就是缓存查询结果 */
    localCache.putObject(key, list);
    if (ms.getStatementType() == StatementType.CALLABLE) {
        localOutputParameterCache.putObject(key, parameter);
    }
    return list;
}

诚的doQuery操作是由SimplyExecutor代理来成功的,该办法吃发生2单子流程,一个凡是SQL参数的安,另一个是SQL查询操作及结果集的卷入。

// SimpleExecutor类
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);

        /* 子流程1: SQL查询参数的设置 */
        stmt = prepareStatement(handler, ms.getStatementLog());

        /* 子流程2: SQL查询操作和结果集封装 */
        return handler.<E>query(stmt, resultHandler);
    } finally {
        closeStatement(stmt);
    }
}

子流程1 SQL查询参数的装置:

先是获取数据库connection连接,然后准备statement,然后就是安装SQL查询中的参数值。打开一个connection连接,在运完后不见面close,而是存储下来,当下次欲开辟连接时便直接返回。

// SimpleExecutor类
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    /* 获取Connection连接 */
    Connection connection = getConnection(statementLog);

    /* 准备Statement */
    stmt = handler.prepare(connection, transaction.getTimeout());

    /* 设置SQL查询中的参数值 */
    handler.parameterize(stmt);
    return stmt;
}

// DefaultParameterHandler类
public void setParameters(PreparedStatement ps) {
    /**
     * 设置SQL参数值,从ParameterMapping中读取参数值和类型,然后设置到SQL语句中
     */
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            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 params
                    value = 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 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 e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                } catch (SQLException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                }
            }
        }
    }
}

子流程2 SQL查询结果集的卷入:

// SimpleExecutor类
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
    PreparedStatement ps = (PreparedStatement) statement;
    // 执行查询操作
    ps.execute();
    // 执行结果集封装
    return resultSetHandler.<E> handleResultSets(ps);
}

// DefaultReseltSetHandler类
public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

    final List<Object> multipleResults = new ArrayList<Object>();

    int resultSetCount = 0;
    /**
     * 获取第一个ResultSet,同时获取数据库的MetaData数据,包括数据表列名、列的类型、类序号等。
     * 这些信息都存储在了ResultSetWrapper中了
     */
    ResultSetWrapper rsw = getFirstResultSet(stmt);

    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
      ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }

    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++;
      }
    }

    return collapseSingleResultList(multipleResults);
  }

ResultSetWrapper是ResultSet的包类,调用getFirstResultSet方法获得第一独ResultSet,同时获取数据库底MetaData数据,包括数据表列名、列的项目、类序号等,这些消息都存储于ResultSetWrapper类中了。然后调用handleResultSet方法来来进展结果集的卷入。

// DefaultResultSetHandler类
private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {
        if (parentMapping != null) {
            handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
        } else {
            if (resultHandler == null) {
                DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
                handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
                multipleResults.add(defaultResultHandler.getResultList());
            } else {
                handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
            }
        }
    } finally {
        // issue #228 (close resultsets)
        closeResultSet(rsw.getResultSet());
    }
}

这里调用handleRowValues方法开展结果值的安装。

// DefaultResultSetHandler类
public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    if (resultMap.hasNestedResultMaps()) {
        ensureNoRowBounds();
        checkResultHandler();
        handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    } else {
        // 封装数据
        handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    }
}

private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
        throws SQLException {
    DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
    skipRows(rsw.getResultSet(), rowBounds);
    while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
        ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
        Object rowValue = getRowValue(rsw, discriminatedResultMap);
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
    }
}

private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    // createResultObject为新创建的对象,数据表对应的类
    Object rowValue = createResultObject(rsw, resultMap, lazyLoader, null);
    if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
        final MetaObject metaObject = configuration.newMetaObject(rowValue);
        boolean foundValues = this.useConstructorMappings;
        if (shouldApplyAutomaticMappings(resultMap, false)) {
            // 这里把数据填充进去,metaObject中包含了resultObject信息
            foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
        }
        foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
        foundValues = lazyLoader.size() > 0 || foundValues;
        rowValue = (foundValues || configuration.isReturnInstanceForEmptyRow()) ? rowValue : null;
    }
    return rowValue;
}

private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
    List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
    boolean foundValues = false;
    if (autoMapping.size() > 0) {
        // 这里进行for循环调用,因为user表中总共有7列,所以也就调用7次
        for (UnMappedColumnAutoMapping mapping : autoMapping) {
            // 这里将esultSet中查询结果转换为对应的实际类型
            final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
            if (value != null) {
                foundValues = true;
            }
            if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
                // gcode issue #377, call setter on nulls (value is not 'found')
                metaObject.setValue(mapping.property, value);
            }
        }
    }
    return foundValues;
}

mapping.typeHandler.getResult会获取查询结果值的实际上类型,比如我们user表中id字段为int类型,那么它们便本着应Java中的Integer类型,然后经过调用statement.getInt(“id”)来获得其int值,其项目为Integer。metaObject.setValue方法会把收获到之Integer值设置及Java类吃的对应字段。

// MetaObject类
public void setValue(String name, Object value) {
    PropertyTokenizer prop = new PropertyTokenizer(name);
    if (prop.hasNext()) {
        MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
            if (value == null && prop.getChildren() != null) {
                // don't instantiate child path if value is null
                return;
            } else {
                metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
            }
        }
        metaValue.setValue(prop.getChildren(), value);
    } else {
        objectWrapper.set(prop, value);
    }
}

metaValue.setValue方法最后会调用到Java类中对承诺数据域的set方法,这样吧即做到了SQL查询结果集的Java类包装过程。最后贴平摆设调用栈到达Java类的set方法中的快照:

MyBatis 2

 

MyBatis缓存

MyBatis提供查询缓存,用于减轻数据库压力,提高性能。MyBatis提供了一级缓存和二级缓存。

MyBatis 3

一级缓存是SqlSession级别之缓存,每个SqlSession对象还有一个哈希表用于缓存数据,不同SqlSession对象期间缓存不共享。同一个SqlSession对象目标执行2不折不扣一律的SQL查询,在第一软询问执行完毕后以结果缓存起来,这样第二任何查询就甭为数据库查询了,直接回缓存结果即可。MyBatis默认是敞开一级缓存的。

二级缓存是mapper级别的缓存,二级缓存是跨SqlSession的,多单SqlSession对象好一起享同一个二级缓存。不同之SqlSession对象实行两蹩脚同的SQL语句,第一涂鸦会面将查询结果开展缓存,第二次等查询直接返回二级缓存中之结果即可。MyBatis默认是休开起来二级缓存的,可以当布置文件中采取如下配置来开二级缓存:

<settings>
    <setting name="cacheEnabled" value="true"/>
</settings>

当SQL语句进行翻新操作(删除/添加/更新)时,会清空对应的缓存,保证缓存中贮存的都是风靡的多寡。MyBatis的二级缓存对细粒度的数据级别之休养存实现不自己,比如如下需求:对商品信息进行缓存,由于商品信息查询访问量大,但是要求用户每次都能够查询最新的商品信息,此时而用mybatis的二级缓存就无法落实当一个商品变化时独自刷新该商品的休息存信息一旦休刷新其它商品之音,因为mybaits的二级缓存区域因mapper为单位划分,当一个商品信息变化会将所有商品信息的缓存数据全部清空。解决此类问题用以事情层根据需要对数码发生针对缓存,具体事情具体落实。

 

网站地图xml地图