使用RedisTemplate存储的key-value乱码解决办法

2022-07-27,,,

前言

以前用Jedis操作redis,而目前手上的项目是用springboot写的,用的RedisTemplate,我就想学习一下,自己新建了一个项目复现一下,连了本地的redis,但是当我执行了存储后,发现了存进去的key和value都是乱码,而且我的key没有中文,不知道为啥乱码,value中有中文,也是乱码,网上找了一下,最后从这篇博客找到了解决办法,感谢感谢!

引的包:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

版本是2.1.0的

service:

在service中将从数据库中查出来的数据存到redis中,关键代码如下:

package com.fanhf.javastudy.mybatistest.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fanhf.javastudy.mybatistest.bean.BondsBean;
import com.fanhf.javastudy.mybatistest.dao.BondsDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fanhf
 * @Description 对redis操作
 * @date 2020-11-20 13:57
 */
@Service
@Slf4j
public class BondsService {
    private static final  String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    @Autowired
    private BondsDao bondsDao;
    @Autowired
    private  RedisTemplate  redisTemplate;

    public List<BondsBean> getBondsList() {
        return bondsDao.getBondsList();
    }

    public BondsBean getBondsListById(BondsBean bondsBean) {
        BondsBean bondsBean1 = bondsDao.getBondsListById(bondsBean);
        String redisKey = String.format("%s:%s","bondId","bondName");
        String value = (String) redisTemplate.opsForValue().get(redisKey);
        List<BondsBean> list = JSON.parseArray(value,BondsBean.class);
        if(null==list){
             list =  new ArrayList<>();
        }
        list.add(bondsBean1);
        String redisValue = JSONObject.toJSONString(list);
        redisTemplate.opsForValue().set(redisKey,redisValue);
        return bondsBean1;
    }
}

controller

    @PostMapping(value = "/bondslistById")
    @ApiOperation(value = "列表展示")
    public BondsBean getBondsListById(@RequestBody BondsBean RequestBody){
        return bondsService.getBondsListById(RequestBody);
    }

BondsBean 如下

@Data
public class BondsBean {
    private  Integer  bondId;
    private  String  bondCode;
    private  String  bondName;
    private  Integer  applyTime;
    private  String  applyNumber;
    private  String  choosedLucklyNumber;
    private  Integer  choosedLucklyTime;
    private  String  givenMoney;
    private  String  profit;
    private  Integer  be_listsTime;
    private  Integer  createTime;
    private  Integer  updateTime;
    }

这个bean是我本地mysql的对应的数据表

mybatis对应的mapper.xml

    <select id="getBondsListById" parameterType="com.fanhf.javastudy.mybatistest.bean.BondsBean" resultType="com.fanhf.javastudy.mybatistest.bean.BondsBean">
          select * from t_bonds where
        <if test="bondId !=null">
            bond_id = #{bondId}
        </if>
        <if test="bondCode !=null or bondCode!=''">
           and  bond_code = #{bondCode}
        </if>
    </select>

存入到redis中的数据

这个Key直接乱码
再看value

解决办法:

从前言中的那篇博客,找到了解决办法,参考了之前项目中的,RedisTemplate加了泛型Object,我加了Object还是不行,于是改成String就好了。

我就只粘贴这两行代码了,其他的在上面已经粘贴过了

    //RedisTemplate一定要加上泛型,而且泛型是String,Object也会乱码
    @Autowired
    private  RedisTemplate<String , String> redisTemplate;

感觉放的代码有点多了[捂脸][捂脸],主要是怕自己说的不明白,让各位看官看的稀里糊涂,所以就索性都展示了一下,需要的自取即可。

也看到了其他的博客,应该也是可以解决的,我还没有尝试,只是觉得稍微有点麻烦[hhhhh]

这篇是写一个class进行编码设置的:使用RedisTemplate存储至缓存数据乱码解决

通过客户端命令设置(不知道 ./redis-cli -raw在哪里执行[捂脸]):redis客户端 (redis-cli) 中文乱码、不解析中文

--------------------------与君共勉--------------------------

本文地址:https://blog.csdn.net/fhf2424045058/article/details/110179975

《使用RedisTemplate存储的key-value乱码解决办法.doc》

下载本文的Word格式文档,以方便收藏与打印。