SpringBoot整合redis(lettuce)

怼烎@ 2024-04-23 21:17 135阅读 0赞

SpringBoot整合redis(lettuce)

  • pom文件导入依赖


    org.springframework.boot
    spring-boot-starter-data-redis
  • application.yml配置

    server:
    port: 8823
    spring:

    redis 要放在spring里,和datasourece同级

    redis:

    1. host: 127.0.0.1
    2. port: 6379
    3. password: 123456
    4. timeout: 3000

    datasource:

    1. username: root
    2. password: root
    3. driver-class-name: com.mysql.cj.jdbc.Driver
    4. url: jdbc:mysql://localhost:3306/hellomybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

    整合mybatis

    mybatis:
    type-aliases-package: com.example.batis.entity
    mapper-locations:

    1. - classpath:mybatis/mapper/*.xml
  • Demo

    package com.example.batis;

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import java.sql.SQLException;

    @SpringBootTest
    class BatisApplicationTests {

  1. @Autowired
  2. private RedisTemplate redisTemplate;
  3. @Test
  4. void contextLoads() throws SQLException {
  5. //redisTemplate 操作不同的数据类型,api和命令行的指令相同
  6. //opsForValue() 操作字符串 类似String
  7. //opsForList 操作List 类似List
  8. //opsForSet
  9. //opsForHash
  10. ·······
  11. //除了基本的操作,常用的方法都可以直接通过redisTemplate操作,如事务和基本的增删改查
  12. redisTemplate.opsForValue().set("key","value");
  13. //获取redis的连接对象(较少用)
  14. RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
  15. connection.flushDb();
  16. connection.flushAll();
  17. redisTemplate.opsForValue().set("key","value");
  18. System.out.println(redisTemplate.opsForValue().get("key"));
  19. }
  20. }
  • 关闭redis的时候要用redis-cli.exe shutdown,暴力关闭redis数据会丢失,这是因为redis保存数据到磁盘不是实时的,可以在redis.conf里配置(save ? ?)

序列化

  • RedisTemplate有默认的配置类,默认使用了jdk的序列化,会对存入的值直接进行转义,因此需要自定义一个RedisTemplate的配置类

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

  1. @Configuration
  2. public class RedisConfig {
  3. //自定义RedisTemplate
  4. @Bean
  5. @SuppressWarnings("all")
  6. public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
  7. //为了开发方便,一般直接使用<String,Object>
  8. RedisTemplate<String,Object>template = new RedisTemplate<String,Object>();
  9. template.setConnectionFactory(factory);
  10. //序列化配置
  11. //Json序列化配置
  12. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  13. ObjectMapper om = new ObjectMapper();
  14. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  15. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  16. jackson2JsonRedisSerializer.setObjectMapper(om);
  17. //String的序列化配置
  18. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  19. //key采用String的序列化方式
  20. template.setKeySerializer(stringRedisSerializer);
  21. //value采用jackson序列化方式
  22. template.setValueSerializer(jackson2JsonRedisSerializer);
  23. //hash的key也采用String序列化方式
  24. template.setHashKeySerializer(stringRedisSerializer);
  25. //hash的value采用jackson序列化方式
  26. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  27. template.afterPropertiesSet();
  28. return template;
  29. }
  30. }
  • 现在在redis里看到的key-value就不是乱码了
  • 工具类:https://download.csdn.net/download/qq_42026590/13087438

发表评论

表情:
评论列表 (有 0 条评论,135人围观)

还没有评论,来说两句吧...

相关阅读