-
依赖配置
<?xml version="1.0" encoding="UTF-8"?>
<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.wangpx.redis</groupId>
<artifactId>redis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--配置redis的资源坐标-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
- String类型的数据操作
@Test
public void testString(){
Jedis jedis = new Jedis("192.168.100.136",6379);
jedis.auth("961220");
jedis.select(3);
jedis.set("name", "张无忌");
System.out.println(jedis.get("name"));
jedis.append("name","张无忌2111");
jedis.getSet("name","张无忌222");
System.out.println(jedis.get("name"));
jedis.mset("name","赵敏","age","12","sex","女");
System.out.println(jedis.mget("name", "age", "sex"));
jedis.incrBy("age",10);
String age = jedis.get("age");
System.out.println(age);
System.out.println(jedis.strlen("name"));
System.out.println(jedis.setrange("name", 123, "赵敏"));
System.out.println(jedis.mget("name", "age", "sex"));
}
- List类型的数据操作
@Test
public void testSet(){
Jedis jedis = new Jedis("192.168.100.136",6379);
jedis.auth("961220");
jedis.select(3);
jedis.sadd("mySet","wangp","han","xia","飞机","老中医");
jedis.sadd("mySet2","wangpx","xx","xia","da飞机","xiao中医");
System.out.println(jedis.scard("mySet"));
System.out.println(jedis.sdiff( "mySet"));
System.out.println(jedis.sdiffstore("mySet2","mySet"));
System.out.println(jedis.sinter("mySet"));
System.out.println(jedis.sinterstore("mySet", "mySet2"));
System.out.println(jedis.sismember("mySet", "xia"));
System.out.println("获得set集合中的所有元素"+jedis.smembers("mySet"));
System.out.println("随机获得set集合中的一个元素,并且删除"+jedis.spop("mySet"));
System.out.println("将元素由a集合移动b集合"+jedis.smove("mySet","mySet2","xia"));
System.out.println(jedis.sunion("mySet", "mySet2"));
System.out.println(jedis.sunionstore("mySet", "mySet2"));
System.out.println("删除set集合中指定的元素:"+jedis.srem("mySet2", "飞机"));
}
- sortedSet类型数据操作
@Test
public void testSortedSet(){
Jedis jedis = new Jedis("192.168.100.136",6379);
jedis.auth("961220");
jedis.select(3);
HashMap<String, Double> map = new HashMap<>();
map.put("zhangfei",80.0);
map.put("zhaoyun",83.0);
map.put("liubang",75.0);
map.put("zhugeliang",60.3);
jedis.zadd("mySortedSet",map);
System.out.println("统计sortedset集合的元素个数"+jedis.zcard("mySortedSet"));
System.out.println(jedis.zcount("mySortedSet", 50.0, 70.0));
System.out.println(jedis.zincrby("mySortedSet", 20, "zhaoyun"));
System.out.println(jedis.zrange("mySortedSet", 0, -1));
}
- Hash类型数据操作
@Test
public void testHash(){
Jedis jedis = new Jedis("192.168.100.136",6379);
jedis.auth("961220");
jedis.select(3);
jedis.hset("myHash2","google","com.google.cn");
HashMap<String, String> map = new HashMap<>();
map.put("zhangfei","牛逼");
map.put("chengyaojin","pangzi");
map.put("louna","xiu");
jedis.hmset("myHash",map);
System.out.println(jedis.hget("myHash2", "google"));
System.out.println(jedis.hkeys("myHash2"));
System.out.println(jedis.hexists("myHash", "zhangfei"));
System.out.println(jedis.hvals("myHash"));
System.out.println(jedis.hdel("myHash", "zhangfei"));
}