使用Jedis进行测试
1.创建Maven项目
2.添加pom.xml文件
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
3.创建一个包com.lianxi.Jedis文件夹和创建一个JedisDemo01的java类
4.测试连接rerdis
/**
* 连接Redis
*/
@Test
public void initCount() {
// 创建Jedis对象,连接Redis服务器
Jedis jedis = new Jedis("127.0.0.1", 6379);
// 设置认证密码
// jedis.auth("root");
// 指定数据库
jedis.select(0);
// 使用ping命令,测试是否连接成功
String result = jedis.ping();
System.out.println(result);
}
Jeids API的操作
/**
* Jeids API的操作
* @author pan_junbiao
*/
@Test
public void jedisAPITest()
{
//创建Jedis实例,连接本地Redis服务
Jedis jedis = new Jedis("127.0.0.1", 6379);
//设置Redis数据库的密码
System.out.println(jedis.auth("123456"));
//获取客户端信息
System.out.println(jedis.getClient());
//清空Redis数据库,相当于执行FLUSHALL命令
System.out.println(jedis.flushAll());
//查看Redis信息,相当于执行INFO命令
System.out.println(jedis.info());
//获取数据库中key的数量,相当于指定DBSIZE命令
System.out.println(jedis.dbSize());
//获取数据库名字
System.out.println(jedis.getDB());
//返回当前Redis服务器的时间,相当于执行TIME命令
System.out.println(jedis.time());
}
Jedis-Api: key
jedis.set("k1", "v1"); jedis.set("k2", "v2"); jedis.set("k3", "v3"); Set<String> keys = jedis.keys("*"); System.out.println(keys.size()); for (String key : keys) { System.out.println(key); } System.out.println(jedis.exists("k1")); System.out.println(jedis.ttl("k1")); System.out.println(jedis.get("k1"));
Jedis-API:String
jedis.mset("str1","v1","str2","v2","str3","v3"); System.out.println(jedis.mget("str1","str2","str3"));
Jedis-API:list
List<String> list = jedis.lrange("mylist",0,-1); for (String element : list) { System.out.println(element); }
Jedis-API:set
jedis.sadd("orders", "order01"); jedis.sadd("orders", "order02"); jedis.sadd("orders", "order03"); jedis.sadd("orders", "order04"); Set<String> smembers = jedis.smembers("orders"); for (String order : smembers) { System.out.println(order); } jedis.srem("orders", "order02");
Jedis-API:hash
jedis.hset("hash1","userName","lisi"); System.out.println(jedis.hget("hash1","userName")); Map<String,String> map = new HashMap<String,String>(); map.put("telphone","13810169999"); map.put("address","atguigu"); map.put("email","abc@163.com"); jedis.hmset("hash2",map); List<String> result = jedis.hmget("hash2", "telphone","email"); for (String element : result) { System.out.println(element); }
Jedis-API:zset
jedis.zadd("zset01", 100d, "z3"); jedis.zadd("zset01", 90d, "l4"); jedis.zadd("zset01", 80d, "w5"); jedis.zadd("zset01", 70d, "z6"); Set<String> zrange = jedis.zrange("zset01", 0, -1); for (String e : zrange) { System.out.println(e); }
示例1:
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class JedisDemo01 {
public static void main(String[] args) {
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
//测试
String ping = jedis.ping();
System.out.println(ping);
}
@Test
void KeyString(){
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
//添加
jedis.set("name","qianling");
//获取
String name = jedis.get("name");
System.out.println(name);
//设置多个key-value
jedis.mset("k1","v1","k2","v2");
List<String> mget = jedis.mget("k1", "k2");
System.out.println(mget);
Set<String> keys = jedis.keys("*");
for (String key : keys) {
System.out.println(key);
}
}
//操作list
@Test
void List(){
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
List<String> list = jedis.lrange("mylist",0,-1);
for (String element : list) {
System.out.println(element);
}
}
//操作set
@Test
void set(){
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.sadd("orders", "order01");
jedis.sadd("orders", "order02&#