四步完成Springboot2.x整合Redis(RedisTemplate)

参考文章:https://blog.csdn.net/qq_38157516/article/details/82356902

 

Demo的github地址:https://github.com/Eternallyc/springboot-redis

 

第一步,首先引入springboot的redis依赖:

 
            org.springframework.boot
            spring-boot-starter-data-redis

第二步,写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 {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate 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 {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private 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;
    }
}

测试结果:

四步完成Springboot2.x整合Redis(RedisTemplate)_第1张图片

 

说明成功了

 

 

 

你可能感兴趣的:(Java,Web,学习)