版本信息

  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

Springboot Mybatis 集成 Redis的更多相关文章

  1. 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战

    大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...

  2. springboot+mybatis集成多数据源MySQL/Oracle/SqlServer

    日常开发中可能时常会遇到一些这样的需求,业务数据库和第三方数据库,两个或多个数据库属于不同数据库厂商,这时候就需要通过配置来实现对数据库实现多源处理.大致说一下我的业务场景,框架本身是配置的sprin ...

  3. SpringBoot项目集成Redis

    一.在pom文件中添加依赖 <!-- 集成redis --> <dependency> <groupId>org.springframework.boot</ ...

  4. SpringBoot+Mybatis集成搭建

    本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...

  5. mybatis集成redis

    系统原生集成的Ehcache, 但是监控需要(version 2.7),Ehcache Monitor http://www.ehcache.org/documentation/2.7/operati ...

  6. springboot+mybatis 用redis作二级缓存

    1.加入相关依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. springboot 2 集成 redis 缓存 序列化

    springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...

  8. SpringBoot中集成redis

    转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...

  9. SpringBoot整合集成redis

    Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...

随机推荐

  1. springboot+内置改为外置tomcat

    1.pom.xml springboot项目利用的是自己内置的tomcat,这边就是不依赖内置的tomcat,将其编译的作用域设置为provided <dependency> <gr ...

  2. Spring框架的AOP编程,最通俗的语言解释,易懂易学

    第七章:AOP技术 前言: AOP技术是根据动态代理设计模式进行的技术.动态代理技术分jdk动态代理和cglib动态代理 jdk动态代理特点: (1)继承java.lang.reflect.proxy ...

  3. Prometheus学习笔记(4)什么是pushgateway???

    目录 一.pushgateway介绍 二.pushgateway的安装运行和配置 三.自定义脚本发送pushgateway 四.使用pushgateway的优缺点 一.pushgateway介绍 pu ...

  4. 你的Mac还安全吗

    MacOS 系统重大安全漏洞:不用密码我也可以玩你的 Macbook Wi-Fi 网络安全保护机制被攻破.Android 泄漏终端设备的用户声音和屏幕活动.iOS 出 bug,Office更是漏洞不断 ...

  5. Cobbler本机使用VM装机配置方法

    一.需要在本地VM虚拟机上安装好Cobbler服务 安装服务及配置方法参见链接:https://www.cnblogs.com/cyleon/p/11460061.html 二.本地配置VM网络 网络 ...

  6. firefox修改user-agent

    让firefox对web服务器伪装成任意浏览器,找一个iphone的useragent,瞬间firefox变身iPhone有木有,一般人我不告诉他嘿嘿 1.firefox地址栏中输入about:con ...

  7. 项目Beta冲刺(团队)——05.27(5/7)

    项目Beta冲刺(团队)--05.27(5/7) 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:记录Beta敏捷冲刺第5 ...

  8. 软件测试之Monkey 初步了解(入门级)

    monkey 介绍 Monkey是Google提供的一个用于稳定性与压力测试的命令行工具.可以运行在模拟器或者实际设备中.它向系统发送伪随机的用户事件(如按键.手势.触摸屏等输入),对软件进行稳定性与 ...

  9. SpringMVC+Mybatis学习

    简单Web项目搭建: 一.流程 1. 导包 n个springMVC: 2个mybatis<其中一个是mybatis-spring>: 3个jackson包: 2. xml配置 web.xm ...

  10. Codeforces 484 E. Sign on Fence

    [传送门] 题意就是给一排围栏,每个围栏都有一个高度,查询区间$\left[l, r\right]$之间长度为$w$的子区间的最小高度的最大值.首先,这个最大值肯定是这个区间里的围栏的某个高度,如果是 ...