[代码笔记] java连接redis&通常操作

8 篇文章 0 订阅
2 篇文章 0 订阅

jedis-2.1.jar包下载地址 点击下载

package cn.guddqs.jredis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;

/**
 * @author guddqs
 * 用来测试java交互Redis
 * 记得导入Junit4jar包
 */
public class JRedis_Connection {

    @Test
    /**
     *  测试连接
     */
    public void test_Conn(){
//      System.out.println("Good");

        try {
            Jedis jedis=new Jedis("127.0.0.1");
            System.out.println("Server is Connectioning.....");

            System.out.println("Server is Running:"+jedis.ping());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    /**
     * 测试操作String
     */
    public void test_String(){

        try {
            Jedis jedis=new Jedis("127.0.0.1");
            System.out.println("Server is Connectioning.....");

            jedis.set("name","Guddqs");

            System.out.println("The Redis's Value For Name Is :"+jedis.get("name"));


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    /**
     * 测试操作list
     */
    public void test_List(){

        try {
            Jedis jedis=new Jedis("127.0.0.1");
            System.out.println("Server is Connectioning.....\n");

            jedis.del("list");
            jedis.lpush("list", "java");
            jedis.lpush("list", "c#");
            jedis.lpush("list", "php");
            jedis.lpush("list", "c");

            List<String> list=jedis.lrange("list", 0, 10);
            for (String string : list) {
                System.out.println(string);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    /**
     * 测试操作Keys
     */
    public void test_Keys(){

        try {
            Jedis jedis=new Jedis("127.0.0.1");
            System.out.println("Server is Connectioning.....\n");

            Set<String> list=jedis.keys("*");
            for (String str : list) {
                System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    /**
     * 测试操作Map
     */
    public void test_Map(){

        try {
            Jedis jedis=new Jedis("127.0.0.1");
            System.out.println("Server is Connectioning.....\n");

            Map<String, String> map=new HashMap<>();
            map.put("name", "Guddqs");
            map.put("age", "19");

            jedis.del("me");
            jedis.hmset("me", map);

            Map<String, String> map1=jedis.hgetAll("me");
            for(String key : map1.keySet()){
                System.out.println(key+"="+map.get(key));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

代码总结

1.连接类似与Socket,可以使用默认端口参数

2.对于所有数据操作, 使用Jedis类对象的方法即可!并且所有方法名与命令几乎一样的! 我测试了List,Map ,对于 Set , Zset 不在重复! 操作这些数据类型的命令笔记在我的上篇博客或者菜鸟教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值