Redis,对于大家来说应该不陌生,是经常使用的开发技术之一。本文将结合实例代码,介绍SpringBoot2整合Redis实现读写操作,感兴趣的小伙伴们可以参考一下
1. 启动 Redis Server
启动 redis server,如下图所示,端口号 6379:
2. 工程实例
2.1 工程目录
工程目录如下图所示:
2.2 pom.xml
引入依赖:
org.springframework.bootspring-boot-starter-data-redis org.apache.commonscommons-pool2
完整 pom.xml 如下所示:
4.0.0 org.springframework.bootspring-boot-starter-parent2.2.2.RELEASE<!-- lookup parent from repository -->com.syrdbtredis-study0.0.1-SNAPSHOTredis-studyDemo project for Spring Boot 1.8 org.springframework.bootspring-boot-starter-data-redis org.apache.commonscommons-pool2 org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-testtest org.junit.vintagejunit-vintage-engine org.springframework.bootspring-boot-maven-plugin
2.3 Java 源文件
启动类,RedisStudyApplication.java:
package com.syrdbt.redis.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedisStudyApplication { public static void main(String[] args) { SpringApplication.run(RedisStudyApplication.class, args); } }
控制器,RedisStudyController.java:
这里使用 SpringBoot 内置的与 Redis 的工具类:RedisTemplate;
除了 RedisTemplate,SpringBoot 还内置了 StringRedisTemplate ;
StringRedisTemplate 只能对 key=String,value=String 的键值对进行操作,RedisTemplate 可以对任何类型的 key-value 键值对操作;StringRedisTemplate 继承了 RedisTemplate。
package com.syrdbt.redis.study.controller; import com.sun.tools.javac.code.Attribute; import com.syrdbt.redis.study.constant.Constant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.Console; /** * @author syrdbt */ @RestController public class RedisStudyController { @Autowired private RedisTemplate redis<b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>Template; /** * 通过 key 获取字符串 */ @GetMapping("/redis/get") public String visitStringByKey(@RequestParam String key) { return (String) redisTemplate.opsForValue().get(Constant.NAMESPACE + ":" + key); } /** * 在 redis 中设置 key/value */ @GetMapping("/redis/set") public String visitStringByKey(@RequestParam String key, @RequestParam String value) { try { redisTemplate.opsForValue().set(Constant.NAMESPACE + ":" + key, value); } catch (Exception e) { return "error"; } return "success"; } }
redis 配置类,RedisConfig.java :
package com.syrdbt.redis.study.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericToStringSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis 配置类 * * @author syrdbt */ @Configuration public class RedisConfig { private final RedisTemplate redisTemplate; @Autowired public RedisConfig(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @Bean @SuppressWarnings("unchecked") public RedisTemplate redisTemplate() { RedisSerializer stringSerializer = new StringRedisSerializer(); RedisSerializer jsonString = new GenericToStringSerializer(Object.class); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(jsonString); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(jsonString); return redisTemplate; } }
常量类用作 redis key 的前缀,Constant.java:
package com.syrdbt.redis.study.constant; /** * @author syrdbt * @date 2019-12-10 */ public class Constant { public static final String NAMESPACE = "REDIS-STUDY"; }
3. 测试
写操作,访问 http://localhost:8080/redis/set?key=name&value=syrdbt 。
读操作,访问http://localhost:8080/redis/get?key=name
4. 问题
整合 redis 的写入和读出的实例已经完成了。
不过还有 2 个问题:
- 我没并没有设置主机号、端口号、用户名、密码就访问了 redis,显然 SpringBoot 默认配置了这些,我本机的redis下载之后没有修改密码等配置,所以才可以访问。
- 正常境况下,不应该直接使用 redisTmplate,应该封装成工具类,这样方便大家使用。
到此这篇关于SpringBoot2整合Redis实现读写操作的文章就介绍到这了,更多相关SpringBoot2 Redis读写操作内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网!
以上就是SpringBoot2整合Redis实现读写操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!