springboot自定义redisTemplate

springboot自定义redisTemplate,第1张

1.添加springboot和redis的相关依赖

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

2.在springboot的yml配置文件中配置redis,注意:此处配置的redis的ip地址以及端口号是自己所安装的redis的ip以及端口号

spring:
  redis:
    port: 6379
    host: 192.168.45.26

3.创建config目录,自定义redisTemplate

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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    //编写我们的redisTemplate
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //为了方便,泛型规定为String,Object类型
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        //序列化 json解析任意对象
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        //使用ObjectMapper转义
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        //String的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);

        //hash的key也采用String 的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        //value采用jackson的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);

        //hash的value也采用jackson的序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

4.创建一个单元测试方法,进行测试。


注意:在进行测试时,一定要先启动redis服务,否则会报错

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lz.pojo.User;
import com.lz.utils.RedisUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class RedisSpringbootApplicationTests {

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;


    @Test
    void contextLoads() {
        /*
             redisTemplate  *** 作不同的数据类型,qpi和我们的指令是一样的
             opsForValue  *** 作字符串,类似于String
             opsForList   *** 作List,类似于List
             opsForHash
             opsForSet
             opsForZSet
             opsForGeo
             opsForHyperLogLog
        */

        // 除了基本的 *** 作,常用的方法都可以通过redisTemplate *** 作,比如事务和基本的CRUD


        // 获取redis的连接对象
//        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
//        connection.flushDb();
//        connection.flushAll();

        redisTemplate.opsForValue().set("key","科比");
        System.out.println(redisTemplate.opsForValue().get("key"));

    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/567778.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-09
下一篇2022-04-09

发表评论

登录后才能评论

评论列表(0条)

    保存