Spring Boot中使用Redis
一、定义工程
创建一个spring boot模块

二、修改pom文件
在pom文件中添加Spring Boot与Redis整合依赖
-
<dependencies>
-
<!--spring boot与redis整合依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
</dependency>
-
-
<!--mybatis与spring boot整合依赖-->
-
<dependency>
-
<groupId>org.mybatis.spring.boot</groupId>
-
<artifactId>mybatis-spring-boot-starter</artifactId>
-
<version>1.3.2</version>
-
</dependency>
-
-
<!--mysql驱动-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
<version>5.1.47</version>
-
</dependency>
-
-
<!-- druid驱动 -->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>druid</artifactId>
-
<version>1.1.12</version>
-
</dependency>
-
-
<!--tomcat内置的JSP解析器-->
-
<dependency>
-
<groupId>org.apache.tomcat.embed</groupId>
-
<artifactId>tomcat-embed-jasper</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
</dependencies>
-
-
<build>
-
<resources>
-
<!--注册dao包下mybatis映射文件为资源目录-->
-
<resource>
-
<directory>src/main/java</directory>
-
<includes>
-
<include>**/*.xml</include>
-
</includes>
-
</resource>
-
<!--注册webapp目录为资源目录-->
-
<resource>
-
<directory>src/main/webapp</directory>
-
<targetPath>META-INF/resources</targetPath>
-
<includes>
-
<include>**/*.*</include>
-
</includes>
-
</resource>
-
</resources>
-
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
三、修改主配置文件
在主配置文件中添加如下内容:
-
# 视图的前辍与后辍
-
spring:
-
mvc:
-
view:
-
prefix: /
-
suffix: .jsp
-
-
# 注册数据源
-
datasource:
-
type: com.alibaba.druid.pool.DruidDataSource
-
driver-class-name: com.mysql.jdbc.Driver
-
url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8
-
username: root
-
password: root
-
-
#编码设置
-
http:
-
encoding:
-
force: true
-
charset: utf-8
-
enabled: true
-
redis:
-
host: 39.97.176.160
-
port: 6379
-
cache:
-
type: redis # 指定缓存类型
-
cache-names: realTimeCache # 指定缓存空间名称,随意
-
-
-
server:
-
tomcat:
-
uri-encoding: UTF-8
-
-
mybatis:
-
# 注册映射文件
-
mapper-locations: classpath:com/abc/dao/*.xml
-
# 注册实体类别名
-
type-aliases-package: com.abc.bean
四、修改实体类Student
由于要将查询的实体类对象缓存到Redis,Redis要求实体类必须序列化。所以需要实体类实现序列化接口

五、修改页面
(1) 修改index页面

(2) 修改welcome.jsp页面

六、 修改Controller类

七、修改Service接口

八、修改Service接口实现类




九、修改Dao接口

十、修改映射文件

十一、测试
注册会清空作用域realTimeCache缓存数据。
查询第一次会缓存信息,后面从Redis获取。

其他
高并发下访问Redis,存在什么问题?存在三个问题
- 缓存穿透:为DB查询为null的数据预设一个值
- 缓存雪崩:提前规划好缓存到期时间
- 热点缓存:双重检测锁机制
Spring Boot中使用Redis的更多相关文章
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...
- Spring Boot中使用Redis小结
Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...
- Spring Boot 中集成 Redis 作为数据缓存
只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看:2.key:缓存 ...
- spring boot 中使用 Redis 与 Log
spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...
- Spring Boot中使用Redis数据库
引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedis.可以通过引入spring-boot-starter-redis来配置依赖关系. <depend ...
- 学习Spring Boot:(十七)Spring Boot 中使用 Redis
前言 Redis 1 是一个由Salvatore Sanfilippo写的key-value存储系统. edis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日 ...
- spring boot中集成Redis
1 pom.xml文件中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作
示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...
随机推荐
- git push超过100M文件处理方法
git push超过100M文件处理方法 github 会在你上传文件大于50M的时候,给予警告 ; 大于100M的时候给出 server reject(拒绝上传) 解决方法 保持单个文件在 100 ...
- vue项目部署到docker中
通过nginx镜像部署 vue项目npm run build打包成dist目录,有的打包会加上版本号 启动 docker 将dist目录通过xftp拷贝到linux服务器上,同目录下新建Dockerf ...
- 解决一个C#中定时任务被阻塞问题
解决一个C#中定时任务被阻塞问题 目录 解决一个C#中定时任务被阻塞问题 1.摘要 2.C#中定时任务的最简方法 3.定时任务阻塞现象 4.阻塞现象原因分析 5.问题解决 1.摘要 本文会介绍一个C# ...
- 到底谁才需要Service Mesh?
本文是Service Mesh系列第1篇 随着云原生时代的来临,使用微服务架构的朋友们开始听到一个新的技术名词--Service Mesh(现在来说已经不算新了). 对于一项新技术的学习,总归绕不过两 ...
- Apache Kyuubi 助力 CDH 解锁 Spark SQL
Apache Kyuubi(Incubating)(下文简称Kyuubi)是⼀个构建在Spark SQL之上的企业级JDBC网关,兼容HiveServer2通信协议,提供高可用.多租户能力.Kyuub ...
- 时间处理,类似"xxxx-xx-xxTxx:xx:xx187+0000"格式
后端返回的时间:"2020-04-24T09:12:51.187+0000" 目标显示时间:2020-04-24 09:12:51 <!DOCTYPE html> ...
- Centos8上安装Nginx
一.Nginx下载 官网:http://nginx.org/ 选择稳定版下载:直接右键复制下载地址即可 命令: wget http://nginx.org/download/nginx-1.20.2. ...
- 什么是CLI、GUI
就是命令行界面command-line interface,也有人称之为字符用户界面(CUI). 通常认为,命令行界面(CLI)没有图形用户界面(GUI)那么方便用户操作. 因为,命令行界面的软件通常 ...
- 8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化
1.制作jenkins docker镜像 1.1 下载jenkins wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.30 ...
- WPS for Linux 字体配置(字体缺失解决办法)
WPS for Linux 字体配置(字体缺失解决办法) 1. 背景 有些linux装完wps后提示"部分字体无法显示"或"some formula symbols mi ...