Springboot Mybatis 集成 Redis
版本信息
- Sprintboot 采用 2.1.7 RELEASE 版本
- Mybatis 采用 2.1.0
- Redis 采用 2.1.6.RELEASE
Redis 的使用
添加 Redis 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
开启缓存
@SpringBootApplication
@EnableCaching
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
- 使用注解 @EnableCaching 开启缓存
添加缓存注解
@Override
@Cacheable(value = "UserCache", key = "'user.getAllUsers'")
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
- 在业务逻辑类的方法上添加 @Cacheable 注解来支持缓存
- @Cacheable 注解中的 key 属性值除了需要被英文双引号引用外,还需要加入英文单引号
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;
}
}
指定 Redis 缓存地址
redis:
host: localhost
port: 6379
- 在 Application.yml 文件指定 Redis 的地址和端口号
清除 Redis 缓存
@Override
@CacheEvict(value = "UserCache", key = "'user.getAllUsers'")
public void delete(Integer id) {
System.out.println("删除了 id 为" + id + "的用户");
userMapper.delete(id);
}
- 当删除了数据库的数据的时候,对 Redis 中缓存的数据进行清除。
- @CacheEvict 注解,与 @Cacheable 注解配置完全相同
启动项目测试缓存
源码地址: github
Springboot Mybatis 集成 Redis的更多相关文章
- 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战
大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...
- springboot+mybatis集成多数据源MySQL/Oracle/SqlServer
日常开发中可能时常会遇到一些这样的需求,业务数据库和第三方数据库,两个或多个数据库属于不同数据库厂商,这时候就需要通过配置来实现对数据库实现多源处理.大致说一下我的业务场景,框架本身是配置的sprin ...
- SpringBoot项目集成Redis
一.在pom文件中添加依赖 <!-- 集成redis --> <dependency> <groupId>org.springframework.boot</ ...
- SpringBoot+Mybatis集成搭建
本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...
- mybatis集成redis
系统原生集成的Ehcache, 但是监控需要(version 2.7),Ehcache Monitor http://www.ehcache.org/documentation/2.7/operati ...
- springboot+mybatis 用redis作二级缓存
1.加入相关依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- springboot 2 集成 redis 缓存 序列化
springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...
- SpringBoot中集成redis
转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...
- SpringBoot整合集成redis
Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...
随机推荐
- Docker以http访问Harbor私有仓库(一)
1 说明 前文Centos7搭建Harbor私有仓库(一)我们成功搭建Harbor,本篇我们主要配置Docker以http方式访问私有仓库 2 Docker配置 2.1 Mac系统 2.1.1 配置D ...
- MySQL 5.7数据库参数优化
连接相关参数 max_connections:允许客户端并发连接的最大数量,默认值是151,一般将该参数设置为500-2000max_connect_errors:如果客户端尝试连接的错误数量超过这个 ...
- java实现快速排序,归并排序
//1.快速排序 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc ...
- 将linux和uboot集成到Android编译框架中
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...
- gitlab修改IP地址及仓库地址
将IP修改为192.168.10.100,操作方法 . 先修改本地的IP地址 vim /etc/sysconfig/network-scripts/ifcfg-eth0TYPE=EthernetBOO ...
- SqlServer中的更新锁(UPDLOCK和READPAST)
UPDLOCK和READPAST,通过UPDLOCK和READPAST的结合我们能够解决许多问题,比如我当前项目中对于更新预约人数,则用到了UPDLOCK和READPAST,因为考虑到并发如果固定预约 ...
- DFS(一):砌墙问题
问题描述 使用两种砖头砌墙,砖头A宽为2,高为1,砖头B宽为3,高为1,用这两种砖头砌一面宽为W,高为H的墙. 为了使墙牢固性高,要求每种砖只能横向摆放,不能竖起来,且除了两侧以外,不能出现上下对齐的 ...
- Codeforces A. Password(KMP的nxt跳转表)
题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 项目Beta冲刺(团队5/7)
项目Beta冲刺(团队) --5/7 作业要求: 项目Beta冲刺(团队) 1.团队信息 团队名 :男上加男 成员信息 : 队员学号 队员姓名 个人博客地址 备注 221600427 Alicesft ...
- React Tutorial: Basic Concept Of React Component---babel, a translator
Getting started with react.js: basic concept of React component 1 What is React.js React, or React.j ...