创建一个java项目,建一个测试类,连接Redis的noSql数据库,用以下5种方法,便可以进行存储了;
import redis.clients.jedis.Jedis;
public class test {
public static void main(String[] args) {
//test1();
//test2();
//test3();
//test4();
test5();
}
private static void test5() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.select(6);
jedis.zadd("mysort",70,"张三");
jedis.zadd("mysort",80,"李四");
jedis.zadd("mysort",90,"王五");
jedis.zadd("mysort",100,"刘六");
System.out.println(jedis.zrangeWithScores("mysort",0,-1));
jedis.close();
}
private static void test4() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.select(5);
jedis.sadd("myset","a");
jedis.sadd("myset","b");
jedis.sadd("myset","c");
//删除
jedis.srem("myset","a");
System.out.println(jedis.smembers("myset"));
jedis.close();
}
private static void test3() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.select(4);
jedis.lpush("mylist","1");
jedis.lpush("mylist","2");
jedis.lpush("mylist","3");
jedis.rpush("mylist","4");
jedis.rpush("mylist","5");
System.out.println(jedis.lrange("mylist",0,-1));
jedis.close();
}
private static void test2() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.select(3);
jedis.hset("zpy","userName","企昂昂");
System.out.println(jedis.hget("zpy","userName"));
System.out.println(jedis.hgetAll("zpy"));
jedis.close();
}
private static void test1() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.select(0);
jedis.set("api","layui镜像网站");
System.out.println("插入的数据:"+jedis.get("api"));
jedis.close();
}
}
|