Jedis操作单机版Redis

本文介绍如何使用Jedis客户端操作Redis数据库,包括直接操作及通过连接池的方式,并提供了具体的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jedis操作单机版Redis

pom.xml

<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>com.bjsxt.demo</groupId>

<artifactId>redis</artifactId>

<version>0.0.1-SNAPSHOT</version>

 

<dependencies>

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.9.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

 

</dependencies>

 

</project>

 

 1 Jedis

需要把jedisjar包添加到工程中,如果是maven需要添加jar包的坐标。

public class JedisTest {

 

@Test

public void testJedisSingle() {

//创建一个jedis的对象。

Jedis jedis = new Jedis("192.168.10.131", 6379);

//调用jedis对象的方法,方法名称和redis的命令一致。

jedis.set("key1", "jedis test");

String string = jedis.get("key1");

System.out.println(string);

//关闭jedis

jedis.close();

}

}

 

2 JedisPool

 

/**

 * 使用连接池

 */

@Test

public void testJedisPool() {

//创建jedis连接池

JedisPool pool = new JedisPool("192.168.10.131", 6379);

//从连接池中获得Jedis对象

Jedis jedis = pool.getResource();

String string = jedis.get("key1");

System.out.println(string);

//关闭jedis对象

jedis.close();

pool.close();

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值