mybatis缓存

     2022-03-18     545

关键词:

MyBatis缓存分为一级缓存和二级缓存

一级缓存

MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任何参数的值都会导致缓存失效)

1)单独使用MyBatis而不继承Spring,使用原生的MyBatis的SqlSessionFactory来构造sqlSession查询,是可以使用以及缓存的,示例代码如下

技术分享
public class Test {
    public static void main(String[] args) throws IOException {
        String config = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        System.out.println(session.selectOne("selectUserByID", 1));
        // 同一个session的相同sql查询,将会使用一级缓存 
        System.out.println(session.selectOne("selectUserByID", 1));
        // 参数改变,需要重新查询
        System.out.println(session.selectOne("selectUserByID", 2));
        // 清空缓存后需要重新查询
        session.clearCache();
        System.out.println(session.selectOne("selectUserByID", 1));
        // session close以后,仍然使用同一个db connection
        session.close();
        session = factory.openSession();
        System.out.println(session.selectOne("selectUserByID", 1));
    }
}
技术分享

输出如下

DEBUG - Openning JDBC Connection
DEBUG - Created connection 10044878.
DEBUG - ooo Using Connection [[email protected]]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ? 
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing
1|test1|19|beijing
DEBUG - ooo Using Connection [[email protected]]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ? 
DEBUG - ==> Parameters: 2(Integer)
2|test2|18|guangzhou
DEBUG - ooo Using Connection [[email protected]]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ? 
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing
DEBUG - Resetting autocommit to true on JDBC Connection [[email protected]]
DEBUG - Closing JDBC Connection [[email protected]]
DEBUG - Returned connection 10044878 to pool.
DEBUG - Openning JDBC Connection
DEBUG - Checked out connection 10044878 from pool.
DEBUG - Setting autocommit to false on JDBC Connection [[email protected]]
DEBUG - ooo Using Connection [[email protected]]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ? 
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing

看以看出来,当参数不变的时候只进行了一次查询,参数变更以后,则需要重新进行查询,而清空缓存以后,参数相同的查询过的SQL也需要重新查询,而且使用的数据库连接是同一个数据库连接,这里要得益于我们在mybatis-config.xml里面的datasource设置

技术分享
<environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">

            </transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
技术分享

注意datasource使用的是POOLED,也就是使用了连接池,所以数据库连接可回收利用,当然这个environment属性在集成spring的时候是不需要的,因为我们需要另外配置datasource的bean.

 

2) 跟Spring集成的时候(使用mybatis-spring)

直接在dao里查询两次同样参数的sql

技术分享
@Repository
public class UserDao extends SqlSessionDaoSupport {
    public User selectUserById(int id) {
        SqlSession session = getSqlSession();
        session.selectOne("dao.userdao.selectUserByID", id);
        // 由于session的实现是SqlSessionTemplate的动态代理实现
        // 它已经在代理类内执行了session.close(),所以无需手动关闭session
        return session.selectOne("dao.userdao.selectUserByID", id);
    }
}
技术分享

观察日志

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [[email protected]]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [[email protected]]
DEBUG - Returning JDBC Connection to DataSource

 

这里执行了2次sql查询,看似我们使用了同一个sqlSession,但是实际上因为我们的dao继承了SqlSessionDaoSupport,而SqlSessionDaoSupport内部sqlSession的实现是使用用动态代理实现的,这个动态代理sqlSessionProxy使用一个模板方法封装了select()等操作,每一次select()查询都会自动先执行openSession(),执行完close()以后调用close()方法,相当于生成了一个新的session实例,所以我们无需手动的去关闭这个session()(关于这一点见下面mybatis的官方文档),当然也无法使用mybatis的一级缓存,也就是说mybatis的一级缓存在spring中是没有作用的.

官方文档摘要

MyBatis SqlSession provides you with specific methods to handle transactions programmatically. But when using MyBatis-Spring your beans will be injected with a Spring managed SqlSession or a Spring managed mapper. That means that Spring will always handle your transactions.

You cannot call SqlSession.commit()SqlSession.rollback() or SqlSession.close() over a Spring managed SqlSession. If you try to do so, a UnsupportedOperationException exception will be thrown. Note these methods are not exposed in injected mapper classes.

 

二级缓存

二级缓存就是global caching,它超出session范围之外,可以被所有sqlSession共享,它的实现机制和mysql的缓存一样,开启它只需要在mybatis的配置文件开启settings里的

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

以及在相应的Mapper文件(例如userMapper.xml)里开启

技术分享
<mapper namespace="dao.userdao">
   ...  select statement ...
       <!-- Cache 配置 -->
    <cache
        eviction="FIFO"
        flushInterval="60000"
        size="512"
        readOnly="true" />
</mapper>
技术分享

需要注意的是global caching的作用域是针对Mapper的Namespace而言的,也就是说只在有在这个Namespace内的查询才能共享这个cache.例如上面的 dao.userdao namespace, 下面是官方文档的介绍

It‘s important to remember that a cache configuration and the cache instance are bound to the namespace of the SQL Map file. Thus, all statements in the same namespace as the cache are bound by it.

例如下面的示例,我们执行两次对同一个sql语句的查询,观察输出日志

技术分享
    @RequestMapping("/getUser")
    public String getUser(Model model) {
        User user = userDao.selectUserById(1);
        model.addAttribute(user);
        return "index";
    }
技术分享

当我们访问两次 /getUser 这个url,查看日志输出

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [dao.userdao]: 0.0
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, [email protected], MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ? 
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [[email protected]]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Invoking afterPropertiesSet() on bean with name ‘index‘
DEBUG - Rendering view [org.springframework.web.servlet.view.JstlView: name ‘index‘; URL [/index.jsp]] in DispatcherServlet with name ‘dispatcher‘
DEBUG - Added model object ‘org.springframework.validation.BindingResult.user‘ of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name ‘index‘
DEBUG - Added model object ‘user‘ of type [bean.User] to request in view with name ‘index‘
DEBUG - Forwarding to resource [/index.jsp] in InternalResourceView ‘index‘
DEBUG - Successfully completed request
DEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘
DEBUG - DispatcherServlet with name ‘dispatcher‘ processing GET request for [/user/getUser]
DEBUG - Looking up handler method for path /user/getUser
DEBUG - Returning handler method [public java.lang.String controller.UserController.getUser(org.springframework.ui.Model)]
DEBUG - Returning cached instance of singleton bean ‘userController‘
DEBUG - Last-Modified value for [/user/getUser] is: -1
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [dao.userdao]: 0.5
DEBUG - Closing non transactional SqlSession [[email protected]]
DEBUG - Rendering view [org.springframework.web.servlet.view.JstlView: name ‘index‘; URL [/index.jsp]] in DispatcherServlet with name ‘dispatcher‘
DEBUG - Added model object ‘org.springframework.validation.BindingResult.user‘ of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name ‘index‘
DEBUG - Added model object ‘user‘ of type [bean.User] to request in view with name ‘index‘
DEBUG - Forwarding to resource [/index.jsp] in InternalResourceView ‘index‘
DEBUG - Successfully completed request

可以看出第二次访问同一个url的时候相同的查询 hit cache了,这就是global cache的作用

The End








































































mybatis一级缓存,mybatis二级缓存,mybatis缓存失效(代码片段)

Mybatis一级缓存,Mybatis二级缓存,Mybatis缓存失效 ================================©Copyright蕃薯耀 2021-06-24https://www.cnblogs.com/fanshuyao/ 一、SpringBoot整合Mybatis1、pom.xml引入依赖(基于SpringBoot:2.3.12.REL 查看详情

转mybatis学习总结——mybatis缓存

【转】MyBatis学习总结(七)——Mybatis缓存一、MyBatis缓存介绍  正如大多数持久层框架一样,MyBatis同样提供了一级缓存和二级缓存的支持一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session,当Sessionflush或close... 查看详情

mybatis缓存

MyBatis缓存MyBatis缓存分为一级缓存和二级缓存一级缓存MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任何参数的值都会导致缓存失效)1)单独使用MyBatis而不继承Spri... 查看详情

mybatis缓存

MyBatis缓存MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。MyBatis系统中默认定义了两级缓存:一级缓存和二级缓存默认情况下,只有一级缓存开启。(SqlSession级别的... 查看详情

mybatis缓存

mybatis的延迟加载和缓存技术开始 mybatis一级缓存  mybatis的二级缓存mybatis默认是没有开启二级缓存的。 开启二级缓存需要在mybatis的全局配置文件sqlMapConfig.xml中加入 除了开启二级缓存开关外,还需要在各自的map... 查看详情

mybatis的二级缓存

文章目录MyBatis的二级缓存MyBatis默认是开启一级缓存的开启MyBatis的二级缓存为什么要开启二级缓存在mybatis的配置文件中进行配置在对应的mapper映射文件中声明相关的DO对象需要实现序列化测试动态代理接口执行更新方法后会清空... 查看详情

mybatis缓存专题-一文彻底搞懂mybatis一级缓存(代码片段)

...不能使用2.什么是一级缓存3.什么情况下会命中一级缓存4.Mybatis的一级缓存机制详解5.MyBatis关闭一级缓存6.Mybatis的一级缓存机制源码分析7.Mybatis的一级缓存机制源码分析图解总结8.一级缓存什么时候被清空?9.一级缓存key是什... 查看详情

mybatis缓存专题-一文彻底搞懂mybatis一级缓存(代码片段)

...不能使用2.什么是一级缓存3.什么情况下会命中一级缓存4.Mybatis的一级缓存机制详解5.MyBatis关闭一级缓存6.Mybatis的一级缓存机制源码分析7.Mybatis的一级缓存机制源码分析图解总结8.一级缓存什么时候被清空?9.一级缓存key是什... 查看详情

mybatis0210mybatis和ehcache缓存框架整合

1.1mybatis和ehcache缓存框架整合一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的,mybatis二级缓存通过ehcache维护缓存数据。1.1.1分布... 查看详情

mybatis缓存机制

mybatis提供了缓存机制减轻数据库压力,提高数据库性能mybatis的缓存分为两级:一级缓存、二级缓存一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效二级缓存是mapper级别的缓存,同一个namespace公用这一个缓存,所... 查看详情

mybatis一级缓存与二级缓存

MyBatis一级缓存  MyBatis一级缓存默认开启,一级缓存为Session级别的缓存,在执行以下操作时一级缓存会清空  1.执行session.clearCache();  2.执行CUD操作  3.session.close();//不是同一个Session对象了 MyBatis二级缓存  需要配... 查看详情

mybatis缓存

MyBatis的缓存正如大多数持久化框架一样,MyBatis提供了一级缓存和二级缓存的支持。1.一级缓存 一级缓存是基于perpetualCache(MyBatis自带)的HashMap本地缓存,作用范围为session域内,当sessionflush或者close之后,该session中所有的cac... 查看详情

mybatis——一级缓存二级缓存

一、Mybatis缓存●MyBatis包含一个非常强大的查询緩存特性,它可以非常方便地定制和配置缓存。绶存可以极大的提升查询效率。●MyBatis系统中默认定义了两级缓存:一级缓存和二级缓存  ○ 默认情况下,只有一级缓存开启... 查看详情

ssm框架mybatis笔记---表之间的关联关系;mybatis事务;mybatis缓存机制;orm概述

MyBatis框架提供两级缓存,一级缓存和二级缓存,默认开启一级缓存。缓存就是为了提交查询效率MyBatis框架提供两级缓存,一级缓存和二级缓存,默认开启一级缓存。缓存就是为了提交查询效率 查看详情

mybatis三

1Mybatis的缓存1.1Mybatis的缓存机制Mybatis包含非常求昂达的查询缓存特性,它可以非常方便的配置和定制。缓存可以极大的提高查询效率。 Mybatis系统中默认定义了一级缓存和二级缓存。①默认情况下,只有一级缓存开启。②二... 查看详情

《深入理解mybatis原理4》mybatis缓存机制的设计与实现

《深入理解mybatis原理》MyBatis缓存机制的设计与实现本文主要讲解MyBatis非常棒的缓存机制的设计原理,给读者们介绍一下MyBatis的缓存机制的轮廓,然后会分别针对缓存机制中的方方面面展开讨论。MyBatis将数据缓存设计成两级结... 查看详情

mybatis缓存理解

缓存接触过hibernate的同学都知道hibernate中有个二级缓存,mybatis也存在二级缓存。mybatis中二级缓存有什么作用呢?mybatis提供查询缓存,可以提高查询效率,减轻数据库的压力。一级缓存一级缓存是session级别的缓存,基于PerpetualCa... 查看详情

mybatis缓存机制

MyBatis提供了一级缓存和二级缓存的支持。一级缓存一级缓存是基于PerpetualCache的HashMap本地缓存;一级缓存的作用域是SqlSession,即不同的SqlSession使用不同的缓存空间;一级缓存的开启和关闭一级缓存是默认开启的;关闭一级缓存... 查看详情