Jedis在Spring boot中的使用教程

本文介绍了在Spring Boot项目中使用Jedis操作Redis的详细步骤,包括添加Jedis依赖、配置文件设置、JedisPool的初始化以及实现RedisService进行CRUD操作。注意在使用后需关闭Jedis连接,避免连接池满的问题。
摘要由CSDN通过智能技术生成

Jedis是redis官方出品的redis java客户端,下面我们在spring boot项目中使用Jedis来操作我们的redis

1.加入Jedis依赖

我是用maven构建的项目,首先加入我们的Jedis依赖如下:

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
 </dependency>

2.配置文件加入我们的配置

#redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.timeout=300
spring.redis.password=123456
spring.redis.pool.max-idle=500
spring.redis.pool.max-wait=500
spring.redis.pool.max-active=1000

这些参数很简单,我只解释下面三个参数。
max-idle 表示最大空闲数
max-wait 表示在连接池用完的时候阻塞多长时间再抛出异常
max-active 表示最大连接数,但是在Jedis高版本用的是max-total
下面我们用一个RedisConfig类来读取我们的配置文件,代码如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:application.properties")
public class RedisConfig {
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.port}")
	private int port;
	@Value("${spring.redis.timeout}")
	private int timeout;//秒
	@Value("${spring.redis.p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值