springboot 整合redis以及存储数据会遇到的问题

1.添加依赖

<!--集成redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<exclusions>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

2.properties或者.yml文件配置redis相关信息

redis:  #redis配置
    database: 1
    host: 127.0.0.1
    port: 6105
    password: 1232132
    jedis:
      pool:
        max-active: 8 #连接池最大连接数
        max-idle: 8 #连接池最大空闲连接数
        max-wait: -1ms #连接池最大阻塞等待时间,默认-1,表示没有限制
        min-idle: 0 #连接池最小空闲连接数

3.控制层代码

package com.iflytek.edu.hnezzhxy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @description
 * @create 2020/07/02 13:11
 */
@RestController
public class TestController {
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redis")
    public void test(){
        redisTemplate.opsForValue().set("a","你好");
        System.out.println(redisTemplate.opsForValue().get("a"));
    }
}

4.postman测试该方法控制台打印"你好"说明整合成功!

5.以上存的是字符串,再将存入对象试试

package com.iflytek.edu.hnezzhxy.controller;

import com.iflytek.edu.hnezzhxy.model.ZsbmLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @description
 * @create 2020/07/02 13:11
 */
@RestController
public class TestController {
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redis")
    public void test(){
        ZsbmLog log=new ZsbmLog();
        log.setOperateType("测试").setOperateIp("1231312").setId(1);
         redisTemplate.opsForValue().set("log",log,2, TimeUnit.MINUTES);
        System.out.println(redisTemplate.opsForValue().get("log"));
    }
}

5.控制台打印不能序列化错误

org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.iflytek.edu.hnezzhxy.model.ZsbmLog]

6.解决办法
在你要保存的对象上实现序列化接口

public class ZsbmUser implements Serializable

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值