springBoot集成redis(jedis)详解

前言:默认电脑已经配置好了redis,不懂redis,点击此处

1.在pom.xml文件中引入对应的配置文件:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>1.4.7.RELEASE</version>
		</dependency>

2.在application.properties文件中加入以下配置:

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
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=0

3.使用单例模式构建redis实例

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
public class RedisHelper {
    private static volatile Jedis jedis;
    private static final String REDIS_IP="127.0.0.1";
    private static final Integer PORT=6379;
    public static Jedis getInstance() {//将构造器声明为私有,只有来自Singleton类内才可以调用构造器
        if (jedis == null) {
            /*如果对象不存在,就利用私有构造器产生 一个实例,
            并把它赋值到uniquerSingleton静态变量中,
            如果我们不需要,它就永远不会产生,这就是延迟实例化*/
            jedis = new Jedis(REDIS_IP,PORT);

        }
        return jedis;
    }
}

4.使用方式

    public static void main(String[] args) {
        Jedis client=RedisHelper.getInstance();
        client.set("ninesun","123456789");
        System.out.println(client.get("ninesun"));
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZNineSun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值