使用java連接Redis,Maven管理操作
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>redis</groupId> <artifactId>redis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.version>5.0.2.RELEASE</spring.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.0.3.RELEASE</version> </dependency> </dependencies> </project>
創(chuàng)建db.properties文件
redis.host=bigdata-hpsk01.huadian.com redis.port=6379 redis.maxIdle=10 redis.minIdle=10 redis.maxTotal=50
書寫工具類
package com.huadian.redisUntil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class JedisPoolUntil {
private static String ADDR = "192.168.59.160";
//Redis的端口號(hào)
private static int PORT = 6379;
/* //可用連接實(shí)例的最大數(shù)目,默認(rèn)值為8;
//如果賦值為-1,則表示不限制;如果pool已經(jīng)分配了maxActive個(gè)jedis實(shí)例,則此時(shí)pool的狀態(tài)為exhausted(耗盡)。
private static int MAX_ACTIVE = 1024;
//控制一個(gè)pool最多有多少個(gè)狀態(tài)為idle(空閑的)的jedis實(shí)例,默認(rèn)值也是8。
private static int MAX_IDLE = 200;
//等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)。如果超過(guò)等待時(shí)間,則直接拋出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;*//*
//在borrow一個(gè)jedis實(shí)例時(shí),是否提前進(jìn)行validate操作;如果為true,則得到的jedis實(shí)例均是可用的;
private static boolean TEST_ON_BORROW = true;*/
private static int MAXTOTAL=20;
private static int MINIDLE=10;
private static int MAXIDLE=15;
private static JedisPool jedisPool = null;
/**
* 初始化Redis連接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAXTOTAL);
config.setMaxIdle(MINIDLE);
config.setMinIdle(MAXIDLE);
jedisPool = new JedisPool(config, ADDR, PORT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取Jedis實(shí)例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 釋放jedis資源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
書寫測(cè)試類
package com.huadian.jedis;
import com.huadian.redisUntil.JedisPoolUntil;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisDemo {
private Jedis jedis = null;
/**
* 單連接
*/
@Test
public void jedisSingleConn(){
String host = "192.168.59.160";
int port = 6379;
Jedis jedis = new Jedis(host, port);
jedis.set("name","張三");
jedis.set("age","18");
String s = jedis.get("name");
String s1 = jedis.get("age");
System.out.println(s);
System.out.println(s1);
}
/**
* 連接池連接
*/
@Test
public void jedisPoolConn(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(20);
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setMaxIdle(15);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.59.160", 6379);
Jedis jedis = jedisPool.getResource();
//取數(shù)據(jù)
String s = jedis.get("name");
String s1 = jedis.get("age");
System.out.println(s);
System.out.println(s1);
}
/**
* 連接池連接
* 工具類
*/
@Test
public void jedisPoolConn1(){
Jedis jedis1 = JedisPoolUntil.getJedis();
//取數(shù)據(jù)
String s = jedis1.get("name");
String s1 = jedis1.get("age");
System.out.println(s);
System.out.println(s1);
}
}
補(bǔ)充知識(shí):JAVA使用Redis所需的MAVEN的POM文件
redis不僅可以通過(guò)命令行進(jìn)行操作,同時(shí)redis也可以通過(guò)javaAPI進(jìn)行操作,這是操作redis所需的依賴
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
</plugins>
</build>
以上這篇使用java連接Redis,Maven管理操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springBoot集成mybatis 轉(zhuǎn)換為 mybatis-plus方式
這篇文章主要介紹了springBoot集成mybatis 轉(zhuǎn)換為 mybatis-plus方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot項(xiàng)目訪問(wèn)圖片的3種實(shí)現(xiàn)方法(親測(cè)可用)
本文主要介紹了springboot項(xiàng)目訪問(wèn)圖片的3種實(shí)現(xiàn)方法,通過(guò)springboot項(xiàng)目訪問(wèn)除項(xiàng)目根目錄之外的其它目錄的圖片,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
完美解決Eclipse導(dǎo)入的項(xiàng)目上有個(gè)紅叉,但不影響項(xiàng)目運(yùn)行的問(wèn)題
這篇文章主要介紹了完美解決Eclipse導(dǎo)入的項(xiàng)目上有個(gè)紅叉,但不影響項(xiàng)目運(yùn)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
SpringBoot訪問(wèn)web中的靜態(tài)資源的方式小結(jié)
這篇文章主要介紹了SpringBoot訪問(wèn)web中的靜態(tài)資源的方式,本文給大家介紹了兩種方式,通過(guò)代碼示例和圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-10-10
java實(shí)現(xiàn)對(duì)map的字典序排序操作示例
這篇文章主要介紹了java實(shí)現(xiàn)對(duì)map的字典序排序操作,結(jié)合實(shí)例形式分析了java參照微信官網(wǎng)算法實(shí)現(xiàn)的字典序排序操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07
Java獲取Excel中圖片所在的行和列坐標(biāo)位置
這篇文章主要介紹了Java獲取Excel中圖片所在的行和列坐標(biāo)位置,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
java調(diào)用process線程阻塞問(wèn)題的解決
這篇文章主要介紹了java調(diào)用process線程阻塞問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java下利用Jackson進(jìn)行JSON解析和序列化示例
本篇文章主要介紹了Java下利用Jackson進(jìn)行JSON解析和序列化示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Java實(shí)現(xiàn)堆排序(大根堆)的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)堆排序(大根堆)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

