版本信息

  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. Python学习日记(三十九) Mysql数据库篇 七

    Mysql函数 高级函数 1.BIN(N) 返回N的二进制编码 ); 执行结果: 2.BINARY(str) 将字符串str转换为二进制字符串 select BINARY('ASCII'); 执行结果 ...

  2. 在现有的mysql主从基础上,搭建mycat实现数据的读写分离

    环境准备:MySQL服务器做好主从复制:centos6的系统 主:192.168.164.131 从:192.168.164.144 mycat服务器:192.168.164.141 a.将MySQL ...

  3. 分析一个UBOOT的方法

    1. 编译完成后可以看到在主目录下生成了uboot.bin文件,为了方便分析,使用如下命令将其反汇编:arm-linux-objdump -D -m arm u-boot > u-boot.as ...

  4. Linux 环境变量配置错误,导致所有命令找不到

    今天配置环境变量,PATH设置出错,所有的命令都找不到了,提示说在/usr/bin/下面可以找到,但是cd过去以后还是不行,自己也在其他路径上找了,还是没找到 而且是公司开发机,怕耽误事儿,着实吓了一 ...

  5. Odoo Controller详解

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826241.html 一:Controller 一般通过继承的形式来创建controller类,继承自od ...

  6. 网络服务-SAMBA

    1. Samba 概述 SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内不同操作系统的计算机之间提供文件及打印机等资源的共享 ...

  7. python 应用开发之-用base64 对图片文件的编码和解码处理

    用base64 对图片文件的编码和解码处理 import base64 def convert(image): f = open(image) img_raw_data = f.read() f.cl ...

  8. 51nod1803 森林直径

    [传送门] 考虑计算直径的 dp 方法. $d[u]$ 表示以 $u$ 为根的子树能 $u$ 能走到的最远距离 $dp[u]$ 表示以 $u$ 为根的子树的直径那么每次dfs一个子节点后$dp[u] ...

  9. python实现用户登录、注册实例

    python面向函数式编程,模拟用户登录验证.注册的代码实现. 主要有以下两个文件: 1.user.txt文档文件,相当于数据库的用户信息表,主要是记录用户名和密码. 注意:1)此文档需要与.py文件 ...

  10. 16-网页,网站,微信公众号基础入门(网页版MQTT,页面控件位置调整入门)

    https://www.cnblogs.com/yangfengwu/p/11200767.html 说一下,只要你java学的很好,那么几乎所有的语言都不在话下了 来看一下样式设置 运行 在左上角感 ...