一、定义工程

创建一个spring boot模块

二、修改pom文件

在pom文件中添加Spring Boot与Redis整合依赖

    1. <dependencies>
      1. <!--spring boot与redis整合依赖-->
        1. <dependency>
          1. <groupId>org.springframework.boot</groupId>
            1. <artifactId>spring-boot-starter-data-redis</artifactId>
              1. </dependency>
                  1. <dependency>
                    1. <groupId>org.projectlombok</groupId>
                      1. <artifactId>lombok</artifactId>
                        1. </dependency>
                            1. <!--mybatis与spring boot整合依赖-->
                              1. <dependency>
                                1. <groupId>org.mybatis.spring.boot</groupId>
                                  1. <artifactId>mybatis-spring-boot-starter</artifactId>
                                    1. <version>1.3.2</version>
                                      1. </dependency>
                                          1. <!--mysql驱动-->
                                            1. <dependency>
                                              1. <groupId>mysql</groupId>
                                                1. <artifactId>mysql-connector-java</artifactId>
                                                  1. <version>5.1.47</version>
                                                    1. </dependency>
                                                        1. <!-- druid驱动 -->
                                                          1. <dependency>
                                                            1. <groupId>com.alibaba</groupId>
                                                              1. <artifactId>druid</artifactId>
                                                                1. <version>1.1.12</version>
                                                                  1. </dependency>
                                                                      1. <!--tomcat内置的JSP解析器-->
                                                                        1. <dependency>
                                                                          1. <groupId>org.apache.tomcat.embed</groupId>
                                                                            1. <artifactId>tomcat-embed-jasper</artifactId>
                                                                              1. </dependency>
                                                                                  1. <dependency>
                                                                                    1. <groupId>org.springframework.boot</groupId>
                                                                                      1. <artifactId>spring-boot-starter-web</artifactId>
                                                                                        1. </dependency>
                                                                                            1. <dependency>
                                                                                              1. <groupId>org.springframework.boot</groupId>
                                                                                                1. <artifactId>spring-boot-starter-test</artifactId>
                                                                                                  1. <scope>test</scope>
                                                                                                    1. </dependency>
                                                                                                      1. </dependencies>
                                                                                                          1. <build>
                                                                                                            1. <resources>
                                                                                                              1. <!--注册dao包下mybatis映射文件为资源目录-->
                                                                                                                1. <resource>
                                                                                                                  1. <directory>src/main/java</directory>
                                                                                                                    1. <includes>
                                                                                                                      1. <include>**/*.xml</include>
                                                                                                                        1. </includes>
                                                                                                                          1. </resource>
                                                                                                                            1. <!--注册webapp目录为资源目录-->
                                                                                                                              1. <resource>
                                                                                                                                1. <directory>src/main/webapp</directory>
                                                                                                                                  1. <targetPath>META-INF/resources</targetPath>
                                                                                                                                    1. <includes>
                                                                                                                                      1. <include>**/*.*</include>
                                                                                                                                        1. </includes>
                                                                                                                                          1. </resource>
                                                                                                                                            1. </resources>
                                                                                                                                                1. <plugins>
                                                                                                                                                  1. <plugin>
                                                                                                                                                    1. <groupId>org.springframework.boot</groupId>
                                                                                                                                                      1. <artifactId>spring-boot-maven-plugin</artifactId>
                                                                                                                                                        1. </plugin>
                                                                                                                                                          1. </plugins>
                                                                                                                                                            1. </build>
                                                                                                                                                          1.  

                                                                                                                                                          三、修改主配置文件

                                                                                                                                                          在主配置文件中添加如下内容:

                                                                                                                                                            1. # 视图的前辍与后辍
                                                                                                                                                              1. spring:
                                                                                                                                                                1. mvc:
                                                                                                                                                                  1. view:
                                                                                                                                                                    1. prefix: /
                                                                                                                                                                      1. suffix: .jsp
                                                                                                                                                                          1. # 注册数据源
                                                                                                                                                                            1. datasource:
                                                                                                                                                                              1. type: com.alibaba.druid.pool.DruidDataSource
                                                                                                                                                                                1. driver-class-name: com.mysql.jdbc.Driver
                                                                                                                                                                                  1. url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8
                                                                                                                                                                                    1. username: root
                                                                                                                                                                                      1. password: root
                                                                                                                                                                                          1. #编码设置
                                                                                                                                                                                            1. http:
                                                                                                                                                                                              1. encoding:
                                                                                                                                                                                                1. force: true
                                                                                                                                                                                                  1. charset: utf-8
                                                                                                                                                                                                    1. enabled: true
                                                                                                                                                                                                      1. redis:
                                                                                                                                                                                                        1. host: 39.97.176.160
                                                                                                                                                                                                          1. port: 6379
                                                                                                                                                                                                            1. cache:
                                                                                                                                                                                                              1. type: redis # 指定缓存类型
                                                                                                                                                                                                                1. cache-names: realTimeCache # 指定缓存空间名称,随意
                                                                                                                                                                                                                      1. server:
                                                                                                                                                                                                                        1. tomcat:
                                                                                                                                                                                                                          1. uri-encoding: UTF-8
                                                                                                                                                                                                                              1. mybatis:
                                                                                                                                                                                                                                1. # 注册映射文件
                                                                                                                                                                                                                                  1. mapper-locations: classpath:com/abc/dao/*.xml
                                                                                                                                                                                                                                    1. # 注册实体类别名
                                                                                                                                                                                                                                      1. type-aliases-package: com.abc.bean
                                                                                                                                                                                                                                    1.  

                                                                                                                                                                                                                                    四、修改实体类Student

                                                                                                                                                                                                                                    由于要将查询的实体类对象缓存到Redis,Redis要求实体类必须序列化。所以需要实体类实现序列化接口

                                                                                                                                                                                                                                    五、修改页面

                                                                                                                                                                                                                                    (1) 修改index页面

                                                                                                                                                                                                                                    (2) 修改welcome.jsp页面

                                                                                                                                                                                                                                    六、 修改Controller类

                                                                                                                                                                                                                                    七、修改Service接口

                                                                                                                                                                                                                                    八、修改Service接口实现类

                                                                                                                                                                                                                                    九、修改Dao接口

                                                                                                                                                                                                                                    十、修改映射文件

                                                                                                                                                                                                                                    十一、测试

                                                                                                                                                                                                                                    注册会清空作用域realTimeCache缓存数据。

                                                                                                                                                                                                                                    查询第一次会缓存信息,后面从Redis获取。

                                                                                                                                                                                                                                    其他

                                                                                                                                                                                                                                    高并发下访问Redis,存在什么问题?存在三个问题

                                                                                                                                                                                                                                    • 缓存穿透:为DB查询为null的数据预设一个值
                                                                                                                                                                                                                                    • 缓存雪崩:提前规划好缓存到期时间
                                                                                                                                                                                                                                    • 热点缓存:双重检测锁机制

                                                                                                                                                                                                                                    Spring Boot中使用Redis的更多相关文章

                                                                                                                                                                                                                                    1. spring boot 中使用redis session

                                                                                                                                                                                                                                      spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...

                                                                                                                                                                                                                                    2. Spring Boot中使用Redis小结

                                                                                                                                                                                                                                      Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...

                                                                                                                                                                                                                                    3. Spring Boot中使用redis的发布/订阅模式

                                                                                                                                                                                                                                      原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...

                                                                                                                                                                                                                                    4. Spring Boot 中集成 Redis 作为数据缓存

                                                                                                                                                                                                                                      只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看:2.key:缓存 ...

                                                                                                                                                                                                                                    5. spring boot 中使用 Redis 与 Log

                                                                                                                                                                                                                                      spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...

                                                                                                                                                                                                                                    6. Spring Boot中使用Redis数据库

                                                                                                                                                                                                                                      引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedis.可以通过引入spring-boot-starter-redis来配置依赖关系. <depend ...

                                                                                                                                                                                                                                    7. 学习Spring Boot:(十七)Spring Boot 中使用 Redis

                                                                                                                                                                                                                                      前言 Redis 1 是一个由Salvatore Sanfilippo写的key-value存储系统. edis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日 ...

                                                                                                                                                                                                                                    8. spring boot中集成Redis

                                                                                                                                                                                                                                      1 pom.xml文件中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...

                                                                                                                                                                                                                                    9. 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作

                                                                                                                                                                                                                                      示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...

                                                                                                                                                                                                                                    随机推荐

                                                                                                                                                                                                                                    1. 【Python接口自动化测试】Postman使用简介

                                                                                                                                                                                                                                      下载地址: http://www.downza.cn/soft/205171.html 工具栏 New: 新建,可以新建Request请求,Collection请求集,环境等等 Import: 导入, ...

                                                                                                                                                                                                                                    2. linux 系统ssh超时设置

                                                                                                                                                                                                                                      1.修改client端的etc/ssh/ssh_config添加以下:(在没有权限改server配置的情形下) ServerAliveInterval 60 #client每隔60秒发送一次请求给se ...

                                                                                                                                                                                                                                    3. Android LayoutInflater(布局填充器)

                                                                                                                                                                                                                                      先来看一下LayoutInflater的基本用法吧,它的用法非常简单,首先需要获取到LayoutInflater的实例,有两种方法可以获取到,第一种写法如下: LayoutInflater layou ...

                                                                                                                                                                                                                                    4. Android学习—下载Android SDK的两种方式

                                                                                                                                                                                                                                      在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...

                                                                                                                                                                                                                                    5. C#长程序(留着看)

                                                                                                                                                                                                                                      using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

                                                                                                                                                                                                                                    6. [hdu6989]Didn't I Say to Make My Abilities Average in the Next Life?!

                                                                                                                                                                                                                                      显然问题即求$\frac{\sum_{x\le l\le r\le y}(\max_{l\le i\le r}a_{i}+\min_{l\le i\le r}a_{i})}{(y-x+2)(y-x+1 ...

                                                                                                                                                                                                                                    7. [atARC114F]Permutation Division

                                                                                                                                                                                                                                      由于是排列,即任意两个数字都各不相同,因此字典序最大的$q_{i}$就是将每一段的第一个数从大到小排序 接下来,考虑第一个元素,也就是每一段开头的最大值,分类讨论: 1.当$p_{1}\le k$时, ...

                                                                                                                                                                                                                                    8. 第一个vue程序

                                                                                                                                                                                                                                      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

                                                                                                                                                                                                                                    9. 多线程01.newThread的方式创建线程

                                                                                                                                                                                                                                      1.java应用程序的main函数是一个线程,是被jvm启动的时候调用,线程的名字叫main 2.实现一个线程,必须创建一个thread实例,override run方法,并且调用start方法. 3 ...

                                                                                                                                                                                                                                    10. dart系列之:时间你慢点走,我要在dart中抓住你

                                                                                                                                                                                                                                      目录 简介 DateTime Duration 总结 简介 时间和日期是我们经常会在程序中使用到的对象.但是对时间和日期的处理因为有不同时区的原因,所以一直以来都不是很好用.就像在java中,为时间和 ...