Spring-boot + redis + jedisPool

本文详细介绍了如何在Spring Boot项目中整合Redis,并使用FastJSON进行数据序列化。通过配置pom依赖、设置Redis连接参数、定义RedisTemplate,实现数据的增删查改操作。最后,通过测试类验证功能正确性。

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

简单的配置Speing-boot + redis

pom文件:

<!--redis-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>RELEASE</version>
		</dependency>
<!-- 转化json的时候使用 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.38</version>
		</dependency>

配置文件:

#redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 连接超时时间(毫秒)
spring.redis.timeout=0
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=1
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1

加载配置类:

@Configuration
//初始化jedisPool
public class RedisConfig {
    @Bean
    public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory){
        StringRedisTemplate template = new StringRedisTemplate(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);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
}

实际操作类:

@Repository
public class UserRedis{
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void add(String key,Long time,User user){
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(user),time, TimeUnit.MINUTES);
    }

    public User get(String key){
        User user = null;
        String userJson = redisTemplate.opsForValue().get(key);
        if(!StringUtils.isEmpty(userJson)){
            user = JSONObject.parseObject(userJson,User.class);

        }
        return user;
    }

    public List<User> getList(String key){
        List<User> ts = null;
        String listJson = redisTemplate.opsForValue().get(key);
        if(!StringUtils.isEmpty(listJson)){
            ts = JSONObject.parseArray(listJson,User.class);
        }
        return ts;
    }

    public void delete(String key){
        redisTemplate.opsForValue().getOperations().delete(key);
    }

}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {

    private Logger logger = LoggerFactory.getLogger(RedisTest.class);
    @Autowired
    private UserRedis userRedis;
    @Test
    public void addOneUser(){
        User user = new User();
        user.setUserName("wzl");
        user.setPassword("wzl");
        userRedis.add(User.class.getName() + ":userByname:" + user.getUserName(),10L,user);
    }
}

这样就ok了

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值