三十、Spring Boot集成redis

本文详细介绍了如何在SpringBoot项目中集成Redis,并演示了基本的字符串键值对操作及复杂对象的存储与读取过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#            Spring Boot集成redis

##1、pom.xml配置


#redis连接
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000000

##2、基于键值对都是String类型
基于键值对都是String类型直接注入:

@Autowired
private  StringRedisTemplate redisTemplate;

就可以使用

测试:

//操作redis,值为String
redisTemplate.opsForValue().set("aa", "11111111");
redisTemplate.opsForValue().set("bb", "22222222");
redisTemplate.opsForValue().set("cc", "33333333");	
System.out.println("============="+redisTemplate.opsForValue().get("cc"));

##3、redis保存对象

package com.yang.redisConfig;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import com.yang.domain.User;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, User> template = new RedisTemplate<String, User>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());//对value进行重写
        return template;
    }
}

package com.yang.redisConfig;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class RedisObjectSerializer implements RedisSerializer<Object> {

    private Converter<Object, byte[]> serializer = new SerializingConverter();
    private Converter<byte[], Object> deserializer = new DeserializingConverter();
    static final byte[] EMPTY_ARRAY = new byte[0];
  
  
	public Object deserialize(byte[] bytes) {
	    if (isEmpty(bytes)) {
	      return null;
	    }
	    try {
	      return deserializer.convert(bytes);
	    } catch (Exception ex) {
	      throw new SerializationException("Cannot deserialize", ex);
	    }
	}
	
	
	public byte[] serialize(Object object) {
	    if (object == null) {
	      return EMPTY_ARRAY;
	    }
	    try {
	      return serializer.convert(object);
	    } catch (Exception ex) {
	      return EMPTY_ARRAY;
	    }
	}
	
	
	private boolean isEmpty(byte[] data) {
	    return (data == null || data.length == 0);
	}

}

测试:

//测试redis存对象
@Autowired
private RedisTemplate<String, User> redisObjectTemplate;

User user=new User();
user.setName("肉哒哒");
user.setTelphone("123437");
redisObjectTemplate.opsForValue().set(user.getName(), user);
User user1=redisObjectTemplate.opsForValue().get(user.getName());
System.out.println("==========:"+user1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值