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

參考文章: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 {

    @Bean
    public 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 {
    @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;
    }
}

測試結果:

 

說明成功了

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章