Spring Boot整合Jedis!!!

导入架包:


     redis.clients
     jedis
     2.9.0

因为要用到单元测试所以导入单元测试的架包:


       org.springframework.boot
       spring-boot-starter-test
       2.3.2

配置文件:application.yml

spring:
  redis:
    jedis:
      pool:
        max-idle: 6    #最大空闲数
        max-active: 10 #最大连接数
        min-idle: 2    #最小空闲数
    port: 6379
    host: 127.0.0.1

 配置类:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 

Project: ican_parent - JedisConfig

*

Powered by scl On 2024-01-23 17:13:03

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Configuration public class JedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}") private int minIdle; @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxActive); jedisPoolConfig.setMinIdle(minIdle); jedisPoolConfig.setMaxTotal(maxActive); return jedisPoolConfig; } @Bean public JedisPool jedisPool(JedisPoolConfig poolConfig) { return new JedisPool(poolConfig, host, port); } }

测试类:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by;

import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 

Project: ican_parent - JedisTest

*

Powered by scl On 2024-01-23 16:56:54

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = {IcanJobApplication.class}) public class JedisTest { @Autowired private JedisPool jedisPool; @Test public void JedisSpringBootTest(){ Jedis jedis = jedisPool.getResource(); jedis.set("user","姬如雪"); String s = jedis.get("user"); System.out.println(s); } }

你可能感兴趣的:(spring,boot,redis,java)