版本信息

  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. Golang: 并发抓取网页内容

    在上一篇中,我们根据命令行的 URL 参数输入,抓取对应的网页内容并保存到本地磁盘,今天来记录一下如何利用并发,来抓取多个站点的网页内容. 首先,我们在上一次代码的基础上稍作改造,使它能够获取多个站点 ...

  2. Django之form主键

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  3. Unity历史版本的文档

    前言 在我们的开发过程中,如果要查找Unity文档,通常会有以下两种方式: 1. 打开Unity的官网,查找文档 2. 查找本地安装的Unity文档 但是Unity官网上的文档,总是当前最新版本的文档 ...

  4. C++学习(12)—— 运算符重载

    运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型 1.加号运算符重载 作用:实现两个自定义数据类型相加的运算 #include <iostream> #i ...

  5. Tensorflow简单实践系列(二):张量

    在上一节中,我们安装 TensorFlow 并运行了最简单的应用,这节我们熟悉 TensorFlow 中的张量. 张量是 TensorFlow 的核心数据类型.数学里面也有张量的概念,但是 Tenso ...

  6. K-string HDU - 4641 (后缀自动机)

    K-string \[ Time Limit: 2000 ms\quad Memory Limit: 131072 kB \] 题意 给出长度为 \(n\) 的字符串,接下来跟着 \(m\) 次操作, ...

  7. rpm命令(转)

    rpm -qa|grep mysql rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受 ...

  8. JavaScript == 与 === 区别

    1.对于 string.number 等基础类型,== 和 === 是有区别的 a)不同类型间比较,== 之比较 "转化成同一类型后的值" 看 "值" 是否相等 ...

  9. uni app 零基础小白到项目实战2

    <template> <scroll-view v-for="(card, index) in list" :key="index"> ...

  10. Java动态调用脚本语言Groovy

    Java动态调用脚本语言Groovy 2019-05-15 目录 0. pom.xml添加依赖1. 使用GroovyShell计算表达式2. 使用GroovyScriptEngine脚本引擎加载Gro ...