一、环境搭建
- 利用SpringBoot实现对于Redis数据的存储与读取
- 需要导入的依赖:
org.springframework.boot spring-boot-starter-data-redis com.alibaba fastjson 1.2.28 - 配置文件application.properties的编写
## 连接Redis,密码默认为空 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=
二、开始实验
- 为了方便测试,选择在SpringBoot的测试类中编写代码进行测试!
@SpringBootTest class Tests { @Autowired private RedisTemplate redisTemplate; @Test public void testRedis(){ redisTemplate.opsForValue().set(JSON.toJSONString("123456"), JSON.toJSONString("111"), 5, TimeUnit.MINUTES); String code = (String) redisTemplate.opsForValue().get(JSON.toJSONString("123456")); System.out.println(code); } } - 注意:在Redis中设置的键值对,最好使用Json格式,否则在存储的过程中,很容易被解析为十六进制表达形式,从而导致无法存储和读取数据!!!
三、运行程序
- 利用RedisDesktopManager工具查看存储情况
- 查看控制台输出情况
四、运行结果
- 由上述运行程序可以看出,成功地将数据存储到Redis中,并且可以成功读取到存入的数据!!!



