SpringBoot操作Jedis案例代碼
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、配置相關(guān)參數(shù)
spring.redis.host=127.0.0.1 spring.redis.database=0 spring.redis.port=6379 spring.redis.timeout=5000
3、JedisPool的設(shè)置
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(); } }
到此這篇關(guān)于SpringBoot操作Jedis的文章就介紹到這了,更多相關(guān)SpringBoot操作Jedis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用
這篇文章主要給大家介紹了關(guān)于JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用,BigInteger是Java中用于表示任意大小整數(shù)的類,它提供了加、減、乘、除等多種運(yùn)算方法,適用于大整數(shù)處理和高精度計(jì)算場景,需要的朋友可以參考下2024-11-11java實(shí)現(xiàn)向有序數(shù)組中插入一個(gè)元素實(shí)例
本篇文章主要介紹了java實(shí)現(xiàn)向有序數(shù)組中插入一個(gè)元素實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java 調(diào)用 HTTP 接口的 7 種方式示例代碼(全網(wǎng)最全指南)
在開發(fā)過程中,調(diào)用 HTTP 接口是最常見的需求之一,本文將詳細(xì)介紹 Java 中 7 種主流的調(diào)用 HTTP 接口的方式,包括每種工具的優(yōu)缺點(diǎn)和完整代碼實(shí)現(xiàn),感興趣的朋友一起看看吧2025-02-02如何獲取MyBatis Plus執(zhí)行的完整的SQL語句
這篇文章主要介紹了如何獲取MyBatis Plus執(zhí)行的完整的SQL語句問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07CAT分布式實(shí)時(shí)監(jiān)控系統(tǒng)使用詳解
這篇文章主要為大家介紹了CAT分布式實(shí)時(shí)監(jiān)控系統(tǒng)介紹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Java中BufferedReader與BufferedWriter類的使用示例
BufferedReader與BufferedWriter分別繼承于Reader和Writer類,分別為字符的讀取和寫入添加緩沖功能,這里我們就來看一下Java中BufferedReader與BufferedWriter類的使用示例:2016-06-06智能 AI 代碼生成工具 Cursor 安裝和使用超詳細(xì)教程
Cursor.so 是一個(gè)集成了 GPT-4 的國內(nèi)直接可以訪問的,優(yōu)秀而強(qiáng)大的免費(fèi)代碼生成器,可以幫助你快速編寫、編輯和討論代碼,這篇文章主要介紹了智能 AI 代碼生成工具 Cursor 安裝和使用介紹,需要的朋友可以參考下2023-05-05