一、定义工程

创建一个spring boot模块

二、修改pom文件

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


  1. <dependencies>
  2. <!--spring boot与redis整合依赖-->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-data-redis</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. </dependency>
  11. <!--mybatis与spring boot整合依赖-->
  12. <dependency>
  13. <groupId>org.mybatis.spring.boot</groupId>
  14. <artifactId>mybatis-spring-boot-starter</artifactId>
  15. <version>1.3.2</version>
  16. </dependency>
  17. <!--mysql驱动-->
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <version>5.1.47</version>
  22. </dependency>
  23. <!-- druid驱动 -->
  24. <dependency>
  25. <groupId>com.alibaba</groupId>
  26. <artifactId>druid</artifactId>
  27. <version>1.1.12</version>
  28. </dependency>
  29. <!--tomcat内置的JSP解析器-->
  30. <dependency>
  31. <groupId>org.apache.tomcat.embed</groupId>
  32. <artifactId>tomcat-embed-jasper</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <resources>
  46. <!--注册dao包下mybatis映射文件为资源目录-->
  47. <resource>
  48. <directory>src/main/java</directory>
  49. <includes>
  50. <include>**/*.xml</include>
  51. </includes>
  52. </resource>
  53. <!--注册webapp目录为资源目录-->
  54. <resource>
  55. <directory>src/main/webapp</directory>
  56. <targetPath>META-INF/resources</targetPath>
  57. <includes>
  58. <include>**/*.*</include>
  59. </includes>
  60. </resource>
  61. </resources>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>

三、修改主配置文件

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


  1. # 视图的前辍与后辍
  2. spring:
  3. mvc:
  4. view:
  5. prefix: /
  6. suffix: .jsp
  7. # 注册数据源
  8. datasource:
  9. type: com.alibaba.druid.pool.DruidDataSource
  10. driver-class-name: com.mysql.jdbc.Driver
  11. url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8
  12. username: root
  13. password: root
  14. #编码设置
  15. http:
  16. encoding:
  17. force: true
  18. charset: utf-8
  19. enabled: true
  20. redis:
  21. host: 39.97.176.160
  22. port: 6379
  23. cache:
  24. type: redis # 指定缓存类型
  25. cache-names: realTimeCache # 指定缓存空间名称,随意
  26. server:
  27. tomcat:
  28. uri-encoding: UTF-8
  29. mybatis:
  30. # 注册映射文件
  31. mapper-locations: classpath:com/abc/dao/*.xml
  32. # 注册实体类别名
  33. 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的更多相关文章

  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. git push超过100M文件处理方法

    git push超过100M文件处理方法 github 会在你上传文件大于50M的时候,给予警告 ; 大于100M的时候给出 server reject(拒绝上传) 解决方法 保持单个文件在 100 ...

  2. vue项目部署到docker中

    通过nginx镜像部署 vue项目npm run build打包成dist目录,有的打包会加上版本号 启动 docker 将dist目录通过xftp拷贝到linux服务器上,同目录下新建Dockerf ...

  3. 解决一个C#中定时任务被阻塞问题

    解决一个C#中定时任务被阻塞问题 目录 解决一个C#中定时任务被阻塞问题 1.摘要 2.C#中定时任务的最简方法 3.定时任务阻塞现象 4.阻塞现象原因分析 5.问题解决 1.摘要 本文会介绍一个C# ...

  4. 到底谁才需要Service Mesh?

    本文是Service Mesh系列第1篇 随着云原生时代的来临,使用微服务架构的朋友们开始听到一个新的技术名词--Service Mesh(现在来说已经不算新了). 对于一项新技术的学习,总归绕不过两 ...

  5. Apache Kyuubi 助力 CDH 解锁 Spark SQL

    Apache Kyuubi(Incubating)(下文简称Kyuubi)是⼀个构建在Spark SQL之上的企业级JDBC网关,兼容HiveServer2通信协议,提供高可用.多租户能力.Kyuub ...

  6. 时间处理,类似"xxxx-xx-xxTxx:xx:xx187+0000"格式

    后端返回的时间:"2020-04-24T09:12:51.187+0000" 目标显示时间:2020-04-24   09:12:51 <!DOCTYPE html> ...

  7. Centos8上安装Nginx

    一.Nginx下载 官网:http://nginx.org/ 选择稳定版下载:直接右键复制下载地址即可 命令: wget http://nginx.org/download/nginx-1.20.2. ...

  8. 什么是CLI、GUI

    就是命令行界面command-line interface,也有人称之为字符用户界面(CUI). 通常认为,命令行界面(CLI)没有图形用户界面(GUI)那么方便用户操作. 因为,命令行界面的软件通常 ...

  9. 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 ...

  10. WPS for Linux 字体配置(字体缺失解决办法)

    WPS for Linux 字体配置(字体缺失解决办法) 1. 背景 有些linux装完wps后提示"部分字体无法显示"或"some formula symbols mi ...