国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

Jedis操作Redis數(shù)據(jù)庫(kù)的方法

2024-07-14 08:40:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了Jedis操作Redis數(shù)據(jù)庫(kù)的具體代碼,供大家參考,具體內(nèi)容如下

關(guān)于NoSQL的介紹不寫(xiě)了,直接上代碼

第一步導(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();  // 最大(小)閑置個(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:

Jedis,Redis

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

Jedis,Redis

保存成功!!

接下來(lái):

我們需要抽取一個(gè)工具類(lèi),方便操作: 

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=30redis.minIdle=10redis.maxTotal=100redis.url=localhostredis.port=6379

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江津市| 怀安县| 依安县| 凉城县| 大名县| 崇阳县| 沂南县| 武威市| 会理县| 嘉义县| 九台市| 永嘉县| 新蔡县| 绥德县| 同仁县| 康定县| 龙山县| 南汇区| 绥化市| 延庆县| 涞水县| 秦皇岛市| 衡东县| 光山县| 额敏县| 新密市| 东乡族自治县| 卓尼县| 鹤庆县| 辽宁省| 岱山县| 乐业县| 宜州市| 濮阳县| 黄浦区| 德化县| 大姚县| 玉屏| 辽宁省| 沁阳市| 宜章县|