zoukankan      html  css  js  c++  java
  • Springboot Mybatis 集成 Redis

    版本信息

    1. Sprintboot 采用 2.1.7 RELEASE 版本
    2. Mybatis 采用 2.1.0
    3. Redis 采用 2.1.6.RELEASE

    Redis 的使用

    1. 添加 Redis 依赖

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>2.1.6.RELEASE</version>
      </dependency>
      
    2. 开启缓存

      @SpringBootApplication
      @EnableCaching
      public class HelloApplication {
          public static void main(String[] args) {
              SpringApplication.run(HelloApplication.class, args);
          }
      } 
      
      • 使用注解 @EnableCaching 开启缓存
    3. 添加缓存注解

          @Override
          @Cacheable(value = "UserCache", key = "'user.getAllUsers'")
          public List<User> getAllUsers() {
              return userMapper.getAllUsers();
          }
      
      • 在业务逻辑类的方法上添加 @Cacheable 注解来支持缓存
      • @Cacheable 注解中的 key 属性值除了需要被英文双引号引用外,还需要加入英文单引号
    4. Bean 实现序列化

      public class User implements Serializable {
      
          private static final long serialVersionUID = 1L;
      
          private Integer id;
          private String username;
          private String address;
      
          public User() {
      
          }
      
          public User(Integer id, String username, String address) {
              this.id = id;
              this.username = username;
              this.address = address;
          }
      
          public Integer getId() {
              return id;
          }
      
          public void setId(Integer id) {
              this.id = id;
          }
      
          public String getUsername() {
              return username == null ? "" : username;
          }
      
          public void setUsername(String username) {
              this.username = username;
          }
      
          public String getAddress() {
              return address == null ? "" : address;
          }
      
          public void setAddress(String address) {
              this.address = address;
          }
      }
      
    5. 指定 Redis 缓存地址

        redis:
          host: localhost
          port: 6379
      
      • 在 Application.yml 文件指定 Redis 的地址和端口号
    6. 清除 Redis 缓存

          @Override
          @CacheEvict(value = "UserCache", key = "'user.getAllUsers'")
          public void delete(Integer id) {
              System.out.println("删除了 id 为" + id + "的用户");
              userMapper.delete(id);
          }
      
      • 当删除了数据库的数据的时候,对 Redis 中缓存的数据进行清除。
      • @CacheEvict 注解,与 @Cacheable 注解配置完全相同
    7. 启动项目测试缓存

    源码地址: github

  • 相关阅读:
    modCount到底是干什么的呢
    Java 8 中的 Streams API 详解
    2017年3月16工作日志【mysql更改字段参数、java8 map()调用方法示例】
    jquery选择器之获取父级元素、同级元素、子元素
    Java8必知必会
    javascript es6 Promise 异步同步的写法(史上最简单的教程了)
    sublime插件开发教程4
    sublime插件开发教程3
    sublime插件开发教程2
    sublime插件开发教程1
  • 原文地址:https://www.cnblogs.com/liyiran/p/11522841.html
Copyright © 2011-2022 走看看