版本信息

  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. MongoDB配置文件及添加用户

    一.参数启动mongodb $ mongod --bind_ip=0.0.0.0 二.配置文件: mongod.conf # 后台运行 fork=true # 数据存储文件目录 dbpath=/roo ...

  2. k8s 学习笔记

    常用的kubectl命令   kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1   --image 指定镜像 - ...

  3. Python的Mixin

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826299.html 一:Mixin模式 Mixin编程是一种开发模式,是一种 将多个不同类中的功能单元的 ...

  4. 将Quartz.NET集成到 Castle中

    原文:https://cloud.tencent.com/developer/article/1030346 Castle是针对.NET平台的一个开源项目,从数据访问框架ORM到IOC容器,再到WEB ...

  5. java多线程的几种实现方式

    java多线程的几种实现方式 1.继承Thread类,重写run方法2.实现Runnable接口,重写run方法,实现Runnable接口的实现类的实例对象作为Thread构造函数的target3.通 ...

  6. fastjson =< 1.2.47 反序列化漏洞复现

    fastjson =< 1.2.47 反序列化漏洞复现 HW期间爆出来一个在hw期间使用的fastjson 漏洞,该漏洞无需开启autoType即可利用成功,建议使用fastjson的用户尽快升 ...

  7. python应用-21根火柴游戏

    """ 21跟火柴 """ from random import randint def main(): total=21 while to ...

  8. 一个使用vue和echarts结合的demo

    前端框架真的很神奇,接触了几天vue.js,用它结合echarts做一个数据分析图,效果如下: 附上源码地址:https://github.com/qingguoYan/orderVue.git

  9. NSData、数据结构与数据转换

    数据结构公式:Data_Structure=(D,R): 只要数据元素与数据(组织关系)能够保持:同一个数据(结构)可以在各种存贮形式间进行转换. 字节流或字符串是所有转化的中间节点(中转站).相当于 ...

  10. 开发(一) ardunio环境配置 针对esp32-cam 更多例程

    第一种  简单版本,针对获取mpu9250数据, http://www.bubuko.com/infodetail-3093785.html 第二种 浮渣版本,针对ESP32获取图像,以及跟多开发例程 ...