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