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. ...
随机推荐
- 树莓派无显示屏连接wifi
在烧好Raspbian系统的TF卡boot分区新建 wpa_supplicant.conf 文件,内容如下(修改自己的WIFI名和密码,key_mgmt根据路由器配置),保存后启动树莓派即可自动连接W ...
- java常见数据结构的时间复杂度总结
- 【转】tf.SessionRunHook使用方法
原文地址:https://blog.csdn.net/mrr1ght/article/details/81011280 .本文有删减. tf.train.SessionRunHook()是一个类:用来 ...
- ROS学习(更新中~)
1.一次把ROS环境变量都自动配置好(即添加到bash会话中)echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc ...
- python基础语法5 函数定义,可变长参数
函数 1.什么是函数 函数就是一种工具. 可以重复调用 2.为什么要用函数 1.防止代码冗(rong)余 2.代码的可读性差 3.怎么用函数 1.定义函数-->制造工具 2.调用函数--> ...
- Entity 类中加了@Id 注解后仍然出现org.hibernate.AnnotationException: No identifier specified for entity 错误
查看网上的资料,应该是报错的实体类com.example.domain.p.User中没有添加加主键的注解@Id,这个是必须的.但是我的实体类中明明已经添加了@Id,为什么还会报这个错误呢? 后来检查 ...
- 函数中,对形参做不加var的全局溢出赋值,可改变形参所指向的实参的本身值
var formateNumArr = function(arr,defaultVal){ var a = []; $.each(arr,function(i,v){ ...
- ie下的透明度,用滤镜filter:alpha
.box{ width:100px; height:100px; background-color:#000; filter:alpha(Opacity=50); opacity: 0.5; }
- js spread object
What’s is the benefit / drawback of these two alternatives? Using object spread options = {...option ...
- [iOS] 利用 NSAttributedString 进行富文本处理
/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...