服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot2整合Redis实现读写操作

SpringBoot2整合Redis实现读写操作

2021-09-28 09:57syrdbt Java教程

Redis,对于大家来说应该不陌生,是经常使用的开发技术之一。本文将结合实例代码,介绍SpringBoot2整合Redis实现读写操作,感兴趣的小伙伴们可以参考一下

1. 启动 Redis Server

启动 redis server,如下图所示,端口号 6379:

SpringBoot2整合Redis实现读写操作

2. 工程实例

 

2.1 工程目录

工程目录如下图所示:

SpringBoot2整合Redis实现读写操作

2.2 pom.xml

引入依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. </dependency>

完整 pom.xml 如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.syrdbt</groupId>
  12. <artifactId>redis-study</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>redis-study</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-redis</artifactId>
  25. </dependency>
  26.  
  27. <dependency>
  28. <groupId>org.apache.commons</groupId>
  29. <artifactId>commons-pool2</artifactId>
  30. </dependency>
  31.  
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36.  
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. <exclusions>
  42. <exclusion>
  43. <groupId>org.junit.vintage</groupId>
  44. <artifactId>junit-vintage-engine</artifactId>
  45. </exclusion>
  46. </exclusions>
  47. </dependency>
  48. </dependencies>
  49.  
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. </plugin>
  56. </plugins>
  57. </build>
  58.  
  59. </project>

2.3 Java 源文件

启动类,RedisStudyApplication.java:

  1. package com.syrdbt.redis.study;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class RedisStudyApplication {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(RedisStudyApplication.class, args);
  11. }
  12.  
  13. }

控制器,RedisStudyController.java

这里使用 SpringBoot 内置的与 Redis 的工具类:RedisTemplate;

除了 RedisTemplate,SpringBoot 还内置了 StringRedisTemplate ;

StringRedisTemplate 只能对 key=String,value=String 的键值对进行操作,RedisTemplate 可以对任何类型的 key-value 键值对操作;StringRedisTemplate 继承了 RedisTemplate。

  1. package com.syrdbt.redis.study.controller;
  2.  
  3. import com.sun.tools.javac.code.Attribute;
  4. import com.syrdbt.redis.study.constant.Constant;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. import java.io.Console;
  12.  
  13. /**
  14. * @author syrdbt
  15. */
  16. @RestController
  17. public class RedisStudyController {
  18. @Autowired
  19. private RedisTemplate redisTemplate;
  20.  
  21. /**
  22. * 通过 key 获取字符串
  23. */
  24. @GetMapping("/redis/get")
  25. public String visitStringByKey(@RequestParam String key) {
  26. return (String) redisTemplate.opsForValue().get(Constant.NAMESPACE + ":" + key);
  27. }
  28.  
  29. /**
  30. * 在 redis 中设置 key/value
  31. */
  32. @GetMapping("/redis/set")
  33. public String visitStringByKey(@RequestParam String key, @RequestParam String value) {
  34. try {
  35. redisTemplate.opsForValue().set(Constant.NAMESPACE + ":" + key, value);
  36. } catch (Exception e) {
  37. return "error";
  38. }
  39. return "success";
  40. }
  41. }

redis 配置类,RedisConfig.java :

  1. package com.syrdbt.redis.study.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.GenericToStringSerializer;
  8. import org.springframework.data.redis.serializer.RedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10.  
  11. /**
  12. * redis 配置类
  13. *
  14. * @author syrdbt
  15. */
  16. @Configuration
  17. public class RedisConfig {
  18.  
  19. private final RedisTemplate redisTemplate;
  20.  
  21. @Autowired
  22. public RedisConfig(RedisTemplate redisTemplate) {
  23. this.redisTemplate = redisTemplate;
  24. }
  25.  
  26. @Bean
  27. @SuppressWarnings("unchecked")
  28. public RedisTemplate<String, Object> redisTemplate() {
  29. RedisSerializer<String> stringSerializer = new StringRedisSerializer();
  30. RedisSerializer<Object> jsonString = new GenericToStringSerializer<>(Object.class);
  31. redisTemplate.setKeySerializer(stringSerializer);
  32. redisTemplate.setValueSerializer(jsonString);
  33. redisTemplate.setHashKeySerializer(stringSerializer);
  34. redisTemplate.setHashValueSerializer(jsonString);
  35. return redisTemplate;
  36. }
  37. }

常量类用作 redis key 的前缀,Constant.java:

  1. package com.syrdbt.redis.study.constant;
  2.  
  3. /**
  4. * @author syrdbt
  5. * @date 2019-12-10
  6. */
  7. public class Constant {
  8. public static final String NAMESPACE = "REDIS-STUDY";
  9. }

3. 测试

写操作,访问 http://localhost:8080/redis/set?key=name&value=syrdbt 。

SpringBoot2整合Redis实现读写操作

读操作,访问http://localhost:8080/redis/get?key=name

SpringBoot2整合Redis实现读写操作

4. 问题

整合 redis 的写入和读出的实例已经完成了。

不过还有 2 个问题:

  • 我没并没有设置主机号、端口号、用户名、密码就访问了 redis,显然 SpringBoot 默认配置了这些,我本机的redis下载之后没有修改密码等配置,所以才可以访问。
  • 正常境况下,不应该直接使用 redisTmplate,应该封装成工具类,这样方便大家使用。

到此这篇关于SpringBoot2整合Redis实现读写操作的文章就介绍到这了,更多相关SpringBoot2 Redis读写操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://xuxiangyang.blog.csdn.net/article/details/104163042

延伸 · 阅读

精彩推荐