- 引入redis的依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
2 java代码操作redis
package com.test;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class RedisTest {
public static void main(String[] args) {
//1.获取连接对象
Jedis jedis = new Jedis("192.168.237.5",6379);//默认连接的是localhost 端口号为6379
//2.操作redis数据库 ----String类型
jedis.set("k1","v1");
jedis.setnx("k1","v1");
jedis.mset("k2","v2","k3","v3");
List<String> mget = jedis.mget("k1", "k2");
System.out.println(mget);
jedis.flushAll();//清除所有数据
//3.操作hash类型
HashMap<String, String> map = new HashMap<>();
map.put("name","xuxu");
map.put("age","18");
map.put("sex","男");
jedis.hset("k1",map);
Map<String, String> mapAll = jedis.hgetAll("k1");
System.out.println(mapAll);
Set<String> keys = jedis.keys("*");
System.out.println(keys);
}
}