参考文章:https://blog.csdn.net/qq_38157516/article/details/82356902
Demo的github地址:https://github.com/Eternallyc/springboot-redis
第一步,首先引入springboot的redis依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步,写application.yml配置
spring:redis:password: #redis密码host: #redis ip地址port: #端口jedis:pool:#最大连接数据库连接数,设 0 为没有限制max-active: 8#最大等待连接中的数量,设 0 为没有限制max-idle: 8#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。max-wait: -1ms#最小等待连接中的数量,设 0 为没有限制min-idle: 0
第三步,写配置类(主要是redis序列化的问题,不然存对象会乱码)
类名:RedisConfiguration
package com.eternallyc.springbootredis.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;@Configuration
public class RedisConfiguration {@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);// 设置value的序列化规则和 key的序列化规则redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}
}
第四步,编写测试类(Controller)
package com.eternallyc.springbootredis.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;@Controller
@RequestMapping("/redis")
public class TestController {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedisTemplate redisTemplate;@RequestMapping("/get/{key}")public ModelAndView getRedis(@PathVariable(name="key") String key){ModelAndView modelAndView= new ModelAndView();redisTemplate.opsForValue().set("directions","123");modelAndView.addObject("list1",redisTemplate.opsForValue().get("directions"));modelAndView.addObject("list", stringRedisTemplate.opsForValue().get(key));modelAndView.setView(new MappingJackson2JsonView());return modelAndView;}
}
测试结果:
说明成功了