修改redis.conf
# 配置绑定ip,作者机子为192.168.100.192,请读者根据实际情况设置
bind 192.168.100.192
#非保护模式
protected-mode no
保存重启
2、操作
2.1 使用idea创建一个maven项目
https://www.jianshu.com/p/042073b7710b
2.2 修改pom.xml
加入下面的依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
2.3 创建测试类
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.100.192", 6379);
//string设置及获取值
jedis.set("name", "ali");
String name = jedis.get("name");
System.out.println(name);
//list设置及获取值
jedis.lpush("list1", "a", "b");
List<String> list1 = jedis.lrange("list1", 0, -1);
System.out.println(list1);
//hash设置及获取值
jedis.hset("hash1", "name", "ali");
jedis.hset("hash1", "sex", "male");
System.out.println(jedis.hget("hash1", "name"));
System.out.println(jedis.hget("hash1", "sex"));
//set设置及获取值
jedis.sadd("set1","1","2","3");
Set<String> set1= jedis.smembers("set1");
System.out.println(set1);
//zset设置及获取值
jedis.zadd("zset1",1,"one");
jedis.zadd("zset1",2,"two");
Set<String> zset1 = jedis.zrange("zset1",0,-1);
System.out.println(zset1);
}
}
至此,我们通过java代码对redis的五种数据类型进行了基本操作。
---------------------
原文地址:https://www.cnblogs.com/hyhy904/p/10992430.html