Redis学习笔记07–SpringBoot整合

  • Post author:
  • Post category:其他


来源参考,b站狂神说:

https://space.bilibili.com/95256449


SpringBoot 操作数据:spring-data jpa jdbc mongodb redis!

说明: 在 SpringBoot2.x 之后,原来使用的jedis 被替换为了 lettuce


jedis :

采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接

池! 更像 BIO 模式


lettuce

: 采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据

了,更像 NIO 模式


源码分析:

@Bean
@ConditionalOnMissingBean(name = "redisTemplate") // 我们可以自己定义一个
redisTemplate来替换这个默认的!
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory
redisConnectionFactory)
throws UnknownHostException {
// 默认的 RedisTemplate 没有过多的设置,redis 对象都是需要序列化!
// 两个泛型都是 Object, Object 的类型,我们后使用需要强制转换 <String, Object>
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean // 由于 String 是redis中最常使用的类型,所以说单独提出来了一
个bean!
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory
redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}

整合测试一下

**导入依赖**
<!-- 操作redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>


配置连接

# 配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379


编写自己的redisUtil类


代码太长,放在这个链接里,有需要自取


https://mp.csdn.net/mp_blog/creation/success/124580638


测试!

@SpringBootTest
class Redis02SpringbootApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Test
    void contextLoad1() {
        // redisTemplate
        // opsForValue 操作string
        // opsForList 操作list
        // ...
        //除了基本的操作,常用的方法都可以通过redistemplate操作,比如书屋,crud

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

        redisTemplate.opsForValue().set("mykey1","123");
        System.out.println(redisTemplate.opsForValue().get("mykey"));
    }

    @Test
    void contextLoads2() throws JsonProcessingException {
        User user = new User();
        user.setUserId("001");
        user.setUserName("tyson");
        String userStr = new ObjectMapper().writeValueAsString(user);
        System.out.println(userStr);
        redisTemplate.opsForValue().set("user",userStr);
        System.out.println(redisTemplate.opsForValue().get("user"));
    }

    @Test
    void contextLoads3() throws JsonProcessingException {
        redisUtil.set("name","tyson");
        System.out.println(redisUtil.get("name"));
        redisUtil.del("name");
        System.out.println(redisUtil.get("name"));
    }
}


编写一个自己的 RedisTemplete


用json的序列化方式,以免出现乱码问题

@Configuration
public class RedisConfig {
    // 编写自己的redistemplate类


    // redis序列化乱码问题,解决
    // 固定模板,拿来可用
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // JSON序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer= new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om =new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, 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采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }
}



版权声明:本文为qq_43589451原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。