Mybatis缓存

Mybatis缓存

  • 什么是Mybatis缓存?

    使用缓存可以减少java应用与数据库的交互次数,从而提升程序的运行效率。比如查询id=1的对象,第一次查询出来后会自动将该对象保存到缓存中,当下一次查询时,直接从缓存中取出对象即可,无需再次访问数据库。

  • Mybatis缓存分类

1.一级缓存:SqlSession级别,默认开启,并且不能关闭。

操作数据库时,需要创建SqlSession对象,在对象中有一个HashMap用于存储缓存数据,不同的SqlSession之间的缓存数据区域是互不影响的

​ 一级缓存的作用域是SqlSession范围的,当在同一个SqlSession中执行两次相同的SQL语句时,第一次执行完毕会将结果保存到缓存中,第二次查询时直接从缓存中获取。

​ 需要注意的是,如果SqlSession执行了DML操作(insert,update,delete),Mybatis必须将缓存清空以保证数据的准确性。

2.二級緩存:Mapper级别,默认关闭,可以开启。

使用二级缓存时,多个Sql'Session使用同一个Mapper的sql语句操作数据库,得到的·数据会存在二级缓存区,同样使用HashMap进行数据存储,相比较于一级缓存,二级缓存的范围更大,多个SqlSession可以共用二级缓存,二级缓存是跨SqlSession的。

代码“:一级缓存

package com.southwind.test;

import com.southwind.entity.Account;
import com.southwind.repository.AccountRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test3 {
    public static void main(String[] args) {
        //加载配置文件
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession =sqlSessionFactory.openSession();
        AccountRepository accountRepository =sqlSession.getMapper(AccountRepository.class);
        Account account = accountRepository.findById(1L);
        System.out.println(account);
        Account account1 = accountRepository.findById(1L);
        System.out.println(account1);
        //进行了两次数据库访问操作,但是实际上只进行了一次,另一次在数据缓存区完成。
    }
}
  • 二级缓存

    1.Mybatis自带的二级缓存

    • 1.config.xml中配置二级缓存
<!--打印SQL-->
        <setting name="logImpl" value="true"/>
<!-- 开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
<!--开启二级缓存 -->
        <setting name="cacheEnabled" value="true"/>
    </settings>
  • 2.Mapper.xml中配置二级缓存。
<cache></cache>
  • 3.实体类中实现序列化接口
package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account implements Serializable {
    private long id;
    private String username;
    private  String password;
    private int age;
}

第三方二级缓存

ehcache二级缓存

  • pom.xml

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.0.0</version>
    </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.0.0</version>
        </dependency>
    
  • config.xml配置二级缓存

    <setting name="logImpl" value="true"/>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="cacheEnabled" value="true"/>
</settings>
  • Mapper.xml中配置二级缓存
    <cache type="org.mybatis.caches.ehcache.EhcacheCache">
<!--缓存创建后,最后一次访问缓存的时间到缓存失效的时间间隔,以秒为单位,设置为一小时-->
        <property name="timeToIdleSeconds" value="3600"/>
<!--缓存创建后,自创建起到缓存失效的时间间隔-->
        <property name="timeToLive" value="3600"/>
<!--缓存回收策略,LRU表示移除近期使用最少的对象-->
        <property name="memoryStoreEvictionPolicy" value="LRU"/>
    </cache>
  • 实体类不需要实现序列化接口。
package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;



@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account  {
    private long id;
    private String username;
    private  String password;
    private int age;
}

posted on 2023-02-16 15:14  张铁蛋666  阅读(34)  评论(0编辑  收藏  举报

导航