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. ...
随机推荐
- go build -tags 的使用
go build 使用tag来实现编译不同的文件 go-tooling-workshop 中关于go build的讲解可以了解到go bulid的一些用法,这篇文章最后要求实现一个根据go bulid ...
- docker中使用阿里云的apt源安装各种实用工具
今天想在docker中安装vim工具,还有其他的软件等等,如果你直接执行apt-get install vim是没有用的,会显示: root@7d43d83fd3a8:/etc/nginx# apt- ...
- 定制你的“魅力”报告--Allure
“人世间是一个大囚笼,每个人都在狱中,砥砺前行.九狱台中的刺,是生活中所要面对的砥砺,是锋利的刺,将自己肉身刺得千疮百孔,将自己的道心刺得千疮百孔.” ---<牧神记·九狱锁道心> 一.简 ...
- Linux 修改时区(PDT修改为CST)
方法1: Linux 修改时区 本文链接:https://blog.csdn.net/Aritem/article/details/79213306 命令:tzselect 按照顺序依次输入5,9,1 ...
- php享元模式(flyweight pattern)
周日一大早,今天要送老婆孩子去火车站, 所以练代码要早点哈. <?php /* The flyweight pattern is about performance and resource r ...
- HDU4183 Pahom on Water(来回走最大流,一个点只经过一次)
题意: 有n个圆,每个圆的中心和半径和一个频率都给定,只有一个频率最高的789为紫色,只有一个最低的400为红色,规则如下: 1.当两个圆严格相交时,且人是从红色到紫色的方向运动时可以由低频率向高频率 ...
- Vue之Action
(1)同步与异步 在 mutation 中混合异步调用会导致你的程序很难调试. 例如,当你调用了两个包含异步回调的 mutation 来改变状态,你怎么知道什么时候回调和哪个先回调呢? 这就是为什么我 ...
- C#中的事件的订阅与发布
认识发布者/订阅者模式 发布者定义一系列事件,并提供一个注册方法: 订阅者向发布者注册自己的事件处理逻辑,供一个可被回调的方法,也就是事件处理程序:当发布者的事件被触发的时候,订阅者将通过回调函数得到 ...
- 【图数据库】Neo4j初次部署踩坑
1.从http://we-yun.com/index.php/blog/versions-56.html或者https://neo4j.com/下载neo4j安装包 2.配置NEO4J_HOME变量, ...
- Java中对象的比较(学习笔记)
1)详细说明对象的比较方式有哪些? ①对象引用的比较("= ="运算符) "= ="是将对象的引用进行比较,实质是比较两个引用变量是否引用同一个对象.注意的点: ...