Jedis操作Redis數(shù)據(jù)庫(kù)的方法
本文實(shí)例為大家分享了Jedis操作Redis數(shù)據(jù)庫(kù)的具體代碼,供大家參考,具體內(nèi)容如下
關(guān)于NoSQL的介紹不寫了,直接上代碼
第一步導(dǎo)包,不多講
基本操作:
package demo;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class Demo {
// 通過(guò)Java程序訪問(wèn)Redis數(shù)據(jù)庫(kù)
@Test
public void test1() {
// 獲得連接對(duì)象
Jedis jedis = new Jedis("localhost", 6379);
// 存儲(chǔ)、獲得數(shù)據(jù)
jedis.set("username", "yiqing");
String username = jedis.get("username");
System.out.println(username);
}
// Jedis連接池獲得jedis連接對(duì)象
@Test
public void test2() {
// 配置并創(chuàng)建redis連接池
JedisPoolConfig poolconfig = new JedisPoolConfig();
// 最大(?。╅e置個(gè)數(shù)
poolconfig.setMaxIdle(30);
poolconfig.setMinIdle(10);
// 最大連接數(shù)
poolconfig.setMaxTotal(50);
JedisPool pool = new JedisPool(poolconfig, "localhost", 6379);
// 獲取資源
Jedis jedis = pool.getResource();
jedis.set("username", "yiqing");
String username = jedis.get("username");
System.out.println(username);
// 關(guān)閉資源
jedis.close();
// 開(kāi)發(fā)中不會(huì)關(guān)閉連接池
// pool.close();
}
}
注意:如果運(yùn)行失敗,那么原因只有一條:沒(méi)有打開(kāi)Redis:

好的,我們可以用可視化工具觀察下:

保存成功??!
接下來(lái):
我們需要抽取一個(gè)工具類,方便操作:
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolUtils {
private static JedisPool pool = null;
static {
// 加載配置文件
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
// 獲得池子對(duì)象
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));// 最大閑置個(gè)數(shù)
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));// 最小閑置個(gè)數(shù)
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));// 最大連接數(shù)
pool = new JedisPool(poolConfig, pro.getProperty("redis.url"),
Integer.parseInt(pro.get("redis.port").toString()));
}
// 獲得Jedis資源
public static Jedis getJedis() {
return pool.getResource();
}
}
在src下新建一個(gè)文件:redis.properties:
redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=localhost redis.port=6379
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信java開(kāi)發(fā)之實(shí)現(xiàn)微信主動(dòng)推送消息
這篇文章主要介紹了微信開(kāi)發(fā)過(guò)程中的使用java實(shí)現(xiàn)微信主動(dòng)推送消息示例,需要的朋友可以參考下2014-03-03
Java?將list集合數(shù)據(jù)按照時(shí)間字段排序的方法
這篇文章主要介紹了Java?將list集合數(shù)據(jù)按照時(shí)間字段排序,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
springboot 設(shè)置局域網(wǎng)訪問(wèn)的實(shí)現(xiàn)步驟
Spring Boot是一個(gè)開(kāi)源Java-based框架,用于創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用,它旨在簡(jiǎn)化Spring應(yīng)用的初始搭建及開(kāi)發(fā)過(guò)程,通過(guò)提供各種自動(dòng)配置的starter包,Spring Boot使得項(xiàng)目配置變得簡(jiǎn)單快速,感興趣的朋友一起看看吧2024-02-02
java實(shí)現(xiàn)文件編碼轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件編碼轉(zhuǎn)換的方法,分享一個(gè)文件編碼轉(zhuǎn)換的工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
深入理解@component與@Configuration注解
這篇文章主要介紹了深入理解@component與@Configuration注解,從Spring3.0,@Configuration用于定義配置類,可替換xml配置文件,被注解的類內(nèi)部包含有一個(gè)或多個(gè)被@Bean注解的方法,這些方法將會(huì)被掃描,并用于構(gòu)建bean定義,初始化Spring容器,需要的朋友可以參考下2023-11-11
SpringBoot集成Beetl后統(tǒng)一處理頁(yè)面異常的方法
這篇文章主要介紹了SpringBoot集成Beetl后統(tǒng)一處理頁(yè)面異常的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

