SpringBoot整合Redis

SpringBoot整合Redis

首先创建一个springboot项目,在创建时勾选spring data下的redis或者导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在使用之前,需要在application.properties中配置redis的参数

# Redis数据库索引(默认为0)
spring.redis.database=0

# redis的地址
spring.redis.host=127.0.0.1

#redis连接端口
spring.redis.port=6379

# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=200

# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms

# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10 

# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0  

# 连接超时时间(毫秒)
spring.redis.timeout=1000ms

编写redis的配置类,主要是绑定连接

@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 为了开发方便,直接使用String -> Object
        // 创建绑定链接
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }
}

然后就可以开始redis的使用了,在使用时装配一个RedisTemplate就可以对redis进行操作,spring对redis的功能进行了分类,在使用之前先选择方法,输入redisTemplate.ops之后,在idea的智能提示中可以看到所有的数据类型

1613375287476

下面是测试代码:

@SpringBootTest
class SpringsecuritytestApplicationTests {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void RedisTest() {
        String str = "Hello, World!";
        this.redisTemplate.opsForValue().set("str", str);
        System.out.println((redisTemplate.opsForValue().get("str")));
    }

}

输出Hello, World!,我们设置的redistemplate是String->Object键值对,那么我们直接传入一个对象又会怎么样呢?

准备一个User类,实现了序列化的接口

@Component
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
    private String name;
    private int age;
}

测试代码:

@SpringBootTest
class SpringsecuritytestApplicationTests {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void RedisTest() {
        User user = new User("YJS", 19);
        this.redisTemplate.opsForValue().set("user", user);
        System.out.println((redisTemplate.opsForValue().get("user")));
    }

}

输出User(name=YJS, age=19)

也可以实现对一个Java对象的存储,但是查看redis本地存储时,却不是我们想看到的:

键:"\xac\xed\x00\x05t\x00\x04user"

值:"\xac\xed\x00\x05sr\x00\rcom.pojo.User4\xb3{\xda{m,\xc1\x02\x00\x02I\x00\x03ageL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x13t\x00\x03YJS"

这样的存储不便于调试,所以需要在配置类中配置自己的序列化器

新配置类:

@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 为了开发方便,直接使用String -> Object
        // 创建绑定链接
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);

        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }
}

重新运行测试代码,看到数据库中的存储

键: "user"

值:"[\"com.pojo.User\",{\"name\":\"YJS\",\"age\":19}]"

这样就便于调试和查看存储对象了

再接着需要可以根据业务需要编写redis的工具类和工具函数,更加便于使用

posted @ 2021-07-24 10:20  骇人的籽  阅读(240)  评论(0编辑  收藏  举报