版本信息

  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. Hive之累计报表生成

    Hive之累计报表生成 1. 原始数据 u01 2019/1/21 5u02 2019/1/23 6u03 2019/1/22 8u04 2019/1/20 3u01 2019/1/23 6u01 2 ...

  2. 随笔记录--Array类型

    前言:除了Object类型之外,Array类型恐怕是ECMAScript中最常用的类型了.而且,ECMAScript中数组与其他多数语言中的数组有很大差别,ECMAScript数组中的每一项可以保存任 ...

  3. .NET CORE 升级3.0遇到的问题the project must provide a value for configuration

    将.NET Core 2.2应用程序迁移到Core 3.0时遇到了the project must provide a value for configuration的问题.原来在.proj项目文件文 ...

  4. Android AMS服务

    继续来研究Android Framework层相关的一些东东,这里是以Android8.0版本的源码进行梳理的,关注的还是其核心流程,不是彻底分析,了解了核心流程是为了了期其大概的原理. Androi ...

  5. C#中的事件的订阅与发布

    认识发布者/订阅者模式 发布者定义一系列事件,并提供一个注册方法: 订阅者向发布者注册自己的事件处理逻辑,供一个可被回调的方法,也就是事件处理程序:当发布者的事件被触发的时候,订阅者将通过回调函数得到 ...

  6. 闲谈关于discuz内核缓存机制

    Discuz! 缓存 Discuz! X2.5 的 config_global.php 中有这样一行代码 $_config['cache']['type'] = 'sql'; 这就是 Discuz! ...

  7. CodeChef Tree Palindromes

    Tree Palindromes Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin cha ...

  8. hive中时间操作(二)

    转:https://blog.csdn.net/qq646748739/article/details/77997276 --Hive中日期函数总结:--1.时间戳函数--日期转时间戳:从1970-0 ...

  9. runloop是一个系统架构、是一个系统

    信号与系统的系统: 事件源.派发系统.事件传播链.事件处理函数: 等组成的整体: 强调的重点是联系与结合.

  10. Socket内核调用数SYSCALL_DEFINE3

    http://blog.chinaunix.net/uid-20788636-id-4408261.html 前言: 对于Linux内核的Socket系列文章都是依据于:Linux-3.14.5的版本 ...