1、Jedis介绍

  (1)Redis不仅是使用命令来操作。现在基本上主流的语言都有客户端支持,比如Java、C、C#、C++、PHP、Node.js、Go等;

  (2)在官方网站里列有一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis等,其中官方推荐使用Jedis和Redissson,在企业中用得最多的是Jedis 。

2、Java连接Redis

  (1)导入jar包

  下载链接Jedis所需jar包 密码:w1j7

  (2)单实例连接

 package com.gzdlh.jedis;

 import org.junit.Test;

 import redis.clients.jedis.Jedis;

 public class JedisTest {
@Test
public void test1() {
// 1、获得Jedis对象,设置IP地址和端口
Jedis jedis = new Jedis("192.168.184.128", 6379); // 2、存储数据
jedis.set("name", "gzdlh"); // 3、获得数据
String name = jedis.get("name");
System.out.println(jedis.get("name")); // 4、释放资源
jedis.close();
}
}

  运行结果:
  

  (3)连接池连接 

 @Test
public void test2() {
// 1、获得连接池配置对象,设置配置项
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(100);// 最大连接数
poolConfig.setMaxIdle(30);// 最大空闲连接数
poolConfig.setMinIdle(10);// 最小空闲连接数
// 2、获得连接池
JedisPool pool = new JedisPool(poolConfig, "192.168.184.128", 6379); // 3、从池子中获取redis的连接资源
Jedis jedis = pool.getResource();
// 4、操作数据库
jedis.set("xxx", "yyy");
System.out.println(jedis.get("xxx")); // 4、释放资源
jedis.close();
pool.close();
}

  (4)JedisPoolUtils

    ①src目录下创建redis.properties

 redis.MaxIdle=30
redis.minIdle=10
redis.maxTotal=100
redis.url=192.168.184.128
redis.port=6379

    ②JedisPoolUtils.java

package com.gzdlh.jedis;

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();
}
// 获得池子对象
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));// 最大连接数
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.MaxIdle").toString()));// 最大空闲连接数
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));// 最小空闲连接数
pool = new JedisPool(poolConfig, pro.getProperty("redis.url"),
Integer.parseInt(pro.get("redis.port").toString()));
} // 获得jedis资源的方法
public static Jedis getJedis() {
return pool.getResource(); } public static void main(String[] args) {
Jedis jedis = getJedis();
System.out.println(jedis.get("xxx"));
}
}

  运行结果:

  

最新文章

  1. 学习实战java虚拟机的计划图
  2. qt 定时器
  3. 使用面向对象对XML进行解析:dom和dom4j的用法
  4. 【转】WebMagic-总体流程源码分析
  5. (转)Delphi工程文件说明
  6. 深入mysql_fetch_row()与mysql_fetch_array()的区别详解
  7. poj 2891 Strange Way to Express Integers (扩展gcd)
  8. lightoj 1007
  9. 【CSS】Beginner6:Border
  10. C#调用VC DLL堆栈不对称
  11. android开发之PreferenceScreen使用详解
  12. DenyHosts限制SSH登录尝试次数
  13. 观点:哪些人适合做FPGA开发?(转)
  14. Oracle高水位2
  15. 十一、VueJs 填坑日记之使用Amaze ui调整列表和内容页面
  16. 如何从零开始学习区块链技术——推荐从以太坊开发DApp开始
  17. R--线性回归诊断(一)
  18. 小程序学习-iPhone X适配
  19. easyui 获取特定页签tab
  20. 关于Unity中的世界坐标和局部坐标

热门文章

  1. vim 插件配置博客记录
  2. 新手git: ssh: connect to host localhost port 22: Connection refused
  3. 怎样预置Android 手机 APK
  4. 出错Can't convert 'WebElement' object to str implicitly
  5. 网页设计——Dreamweaver
  6. sql server中的悲观锁和乐观锁
  7. 杂项-地图:LBS
  8. 05.使用jdk发布webservice服务
  9. curl ,post,get (原创)
  10. 写个js 分页玩玩(原创)