学习笔记:Springboot整合Redis(精简版)

软件部分

redis服务压缩包(windows版):Redis-x64-3.2.100.zip

链接:https://pan.baidu.com/s/1fGdrmCjnj3bXKnA7GanoGw 
提取码:1111

 亲情提示:可将server发送快捷方式到桌面,方便调用

redis可视化软件:redis-desktop-manager-0.8.8.384

链接:https://pan.baidu.com/s/1b_9OI_6Oo1WeRTgipyMlAw 
提取码:1111

无脑下一步(选下安装路径,省的c盘爆满)

 代码部分

第一步:引依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-redis</artifactId>
   <version>1.4.7.RELEASE</version>
</dependency>

第二步:配配置

spring:
  redis:
    host: 127.0.0.1
    # port: 6379
    # password: 
    # database: 0

第三步:test测试

1.引依赖

<!-- spring-boot test -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 
    junit 
    起先我没引入这个依赖,然后那个@Runwith就引不了
-->
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.13.2</version>
   <scope>test</scope>
</dependency>

2.写test类

package com.my;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
public class Test {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 因为测试类名和test注解重名,所以引入注解会带上全限定名
     * 不引入test会报错 ↓
     * org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.my.Test':
     */
    @org.junit.Test
    public void test() {
        // 测试value
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name",123);
        Object name = valueOperations.get("name");
        System.out.println(name);
        // 测试hash
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("dictionary","querySwitch",true);
        Object querySwitch = hashOperations.get("dictionary", "querySwitch");
        System.out.println(querySwitch);
    }
}

3.运行结果

没有毛病

但是当我想打开可视化软件看下存储的结果和结构的时候

 这咋都是乱码?

只能借助度娘了

第四步:Redis配置

使用RedisTemplate存储至缓存数据乱码解决_呢喃北上的博客-CSDN博客

网上有很多类似的博客,贴上其中一个大佬的博客↑↑↑

增加一个配置类 RedisConfig

package com.my.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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * TODO redis配置类
 * @author 清风
 * @date 2021年11月10日
 */
@Configuration
public class RedisConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public RedisTemplate redisTemplateInit() {
        // utf-8
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // jackson2
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置序列化value
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // 设置序列化hash
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }
}

 一开始我也是都用的StringRedisSerializer的序列化方式,但是它会将value都转成string,当你存string以外类型时,会出现转换问题

所以只有key用的是StringRedisSerializer,value用的都是GenericJackson2JsonRedisSerializer

解决乱码问题


后来了解到,其实不是乱码,而是RedisTemplate默认的jdk序列化策略,虽然暂时不影响存取值,但是不利于排查,感觉还是改成后面这样的比较好。

感谢大家的支持和指教,希望可以一起学习进步♠♠♠

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值