SpringBoot操作Jedis案例代码

 更新时间:2023年08月09日 14:57:16   作者:242030  
这篇文章主要介绍了SpringBoot操作Jedis案例代码,代码部分包括pom依赖、配置相关参数、JedisPool的设置,代码简单易懂对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

SpringBoot操作Jedis

1、pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-jedis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-jedis</name>
    <description>spring-boot-jedis</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、配置相关参数

spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.port=6379
spring.redis.timeout=5000

3、JedisPool的设置

package com.example.springbootjedis.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * @author zhangshixing
 * @date 2021年11月06日 11:38
 */
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisConfig {
    private String host;
    private int port;
    private int timeout;
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public int getTimeout() {
        return timeout;
    }
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(0);
        jedisPoolConfig.setMaxWaitMillis(5000);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
        return jedisPool;
    }
}

4、启动类

package com.example.springbootjedis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJedisApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootJedisApplication.class, args);
	}
}

5、测试

package com.example.springbootjedis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@SpringBootTest
class SpringBootJedisApplicationTests {
	@Autowired
	private JedisPool jedisPool;
	@Test
	void contextLoads() {
		Jedis jedis = jedisPool.getResource();
		System.out.println(jedis);
		String keyName = "FirstInfo";
		String fieldName = "redisDemo";
		String str = "hello";
		jedis.hset(keyName,fieldName, str);
		jedis.close();
	}
}

在这里插入图片描述

到此这篇关于SpringBoot操作Jedis的文章就介绍到这了,更多相关SpringBoot操作Jedis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaAPI中BigInteger、BigDecimal的使用方法及应用

    JavaAPI中BigInteger、BigDecimal的使用方法及应用

    这篇文章主要给大家介绍了关于JavaAPI中BigInteger、BigDecimal的使用方法及应用,BigInteger是Java中用于表示任意大小整数的类,它提供了加、减、乘、除等多种运算方法,适用于大整数处理和高精度计算场景,需要的朋友可以参考下
    2024-11-11
  • java实现向有序数组中插入一个元素实例

    java实现向有序数组中插入一个元素实例

    本篇文章主要介绍了java实现向有序数组中插入一个元素实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • springboot的jar包如何启用外部配置文件

    springboot的jar包如何启用外部配置文件

    本文主要介绍了springboot的jar包如何启用外部配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java 调用 HTTP 接口的 7 种方式示例代码(全网最全指南)

    Java 调用 HTTP 接口的 7 种方式示例代码(全网最全指南)

    在开发过程中,调用 HTTP 接口是最常见的需求之一,本文将详细介绍 Java 中 7 种主流的调用 HTTP 接口的方式,包括每种工具的优缺点和完整代码实现,感兴趣的朋友一起看看吧
    2025-02-02
  • 如何获取MyBatis Plus执行的完整的SQL语句

    如何获取MyBatis Plus执行的完整的SQL语句

    这篇文章主要介绍了如何获取MyBatis Plus执行的完整的SQL语句问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • CAT分布式实时监控系统使用详解

    CAT分布式实时监控系统使用详解

    这篇文章主要为大家介绍了CAT分布式实时监控系统介绍详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Java中BufferedReader与BufferedWriter类的使用示例

    Java中BufferedReader与BufferedWriter类的使用示例

    BufferedReader与BufferedWriter分别继承于Reader和Writer类,分别为字符的读取和写入添加缓冲功能,这里我们就来看一下Java中BufferedReader与BufferedWriter类的使用示例:
    2016-06-06
  • Java中常用的9种文件下载方法总结

    Java中常用的9种文件下载方法总结

    下载文件在我们项目很常见,有下载视频、文件、图片、附件、导出Excel等,所以本文为大家整理了9中Java中常用的文件下载方式,希望对大家有所帮助
    2023-09-09
  • CCF考试试题之门禁系统java解题代码

    CCF考试试题之门禁系统java解题代码

    这篇文章主要为大家详细介绍了CCF考试试题之门禁系统java解题代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • 智能 AI 代码生成工具 Cursor 安装和使用超详细教程

    智能 AI 代码生成工具 Cursor 安装和使用超详细教程

    Cursor.so 是一个集成了 GPT-4 的国内直接可以访问的,优秀而强大的免费代码生成器,可以帮助你快速编写、编辑和讨论代码,这篇文章主要介绍了智能 AI 代码生成工具 Cursor 安装和使用介绍,需要的朋友可以参考下
    2023-05-05

最新评论