1、引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2、在application.yml中添加redis服务端的配置信息
核心:ip地址+端口+访问密码
spring:redis:host: 39.107.221.166port: 6379password: Lnxxxyyy123456
3、通过Springboot封装好的RedisTemplate StringRedisTemplate操作redis
@SpringBootTestclass RedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testvoid testStringSet() {redisTemplate.opsForValue().set("msg","hello lianglin");stringRedisTemplate.opsForValue().set("strmsg","hello lianglin strmsg");}@Testvoid testStringGet() {String str1 = (String)redisTemplate.opsForValue().get("msg");System.out.println(str1);String str2 = stringRedisTemplate.opsForValue().get("strmsg");System.out.println(str2);}}
操作结果可以发现
1、RedisTemplate操作数据是无法可视化的,StringRedisTemplate操作的数据是可以可视化的。
2、RedisTemplate ,RedisTemplate之间的的数据是不能互通的,即RedisTemplate设置的key-value不能通过 RedisTemplate来获取,因为这两个用于操作redis的客户端类key的序列化机制不一致。

文章转载自梁霖编程工具库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




