【Redis】Redis进行java操作

目录

jar包

连接以及关闭redis客户端

操作string类型数据

操作hash列表类型数据

操作list类型数据

操作set类型的数据


  • jar包


        
            redis.clients
            jedis
            2.9.0
        
        
            junit
            junit
            4.12
            test
        
        
            org.testng
            testng
            6.14.3
            test
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.0
                
                    1.8
                    1.8
                    UTF-8
                    
                
            
        
    

 

  • 连接以及关闭redis客户端

public void redisConnectionPool(){
    //创建redis客户端对象
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(10);
    //最大等待时间
    config.setMaxWaitMillis(3000);
    config.setMaxTotal(50);
    config.setMinIdle(5);
    JedisPool jedisPool = new JedisPool(config, "node01", 6379);
}

@AfterTest
public void closePool(){
    //关闭redis客户端
    jedisPool.close();
}

 

  • 操作string类型数据

public void addStr(){
    Jedis resource = jedisPool.getResource();
    //添加
    resource.set("jediskey", "jedisvalue");
    //查询
    String jediskey = resource.get("jediskey");
    System.out.println(jediskey);
    //修改
    resource.set("jediskey","jedisvalueUpdate");
    //删除
    resource.del("jediskey");
    //实现整型数据的增长操作
    resource.incr("jincr");
    resource.incrBy("jincr",3);
    String jincr = resource.get("jincr");
    System.out.println(jincr);
    resource.close();
}

 

  • 操作hash列表类型数据

public void hashOperate(){
    Jedis resource = jedisPool.getResource();
    //添加数据
    resource.hset("jhsetkey","jmapkey","jmapvalue");
    resource.hset("jhsetkey","jmapkey2","jmapvalue2");
    //获取所有数据
    Map jhsetkey = resource.hgetAll("jhsetkey");
    for (String s : jhsetkey.keySet()) {
        System.out.println(s);
    }
    //修改数据
    resource.hset("jhsetkey","jmapkey2","jmapvalueupdate2");
    Map jhsetkey2 = resource.hgetAll("jhsetkey");
    for (String s : jhsetkey2.keySet()) {
        System.out.println("修改数据打印"+s);
    }
    //删除数据
    resource.del("jhsetkey");

    Set jhsetkey1 = resource.keys("jhsetkey");
    for (String result : jhsetkey1) {
        System.out.println(result);
    }
}

 

  • 操作list类型数据

public void listOperate(){
    Jedis resource = jedisPool.getResource();
    //从左边插入元素
    resource.lpush("listkey","listvalue1","listvalue1","listvalue2");

    //从右边移除元素
    resource.rpop("listkey");
    //获取所有值
    List listkey = resource.lrange("listkey", 0, -1);
    for (String s : listkey) {
        System.out.println(s);
    }
    resource.close();
}

 

  • 操作set类型的数据

public void setOperate(){
    Jedis resource = jedisPool.getResource();
    //添加数据
    resource.sadd("setkey", "setvalue1", "setvalue1", "setvalue2", "setvalue3");
    //查询数据
    Set setkey = resource.smembers("setkey");
    for (String s : setkey) {
        System.out.println(s);
    }
    //移除掉一个数据
    resource.srem("setkey","setvalue3");
    resource.close();
}

 

你可能感兴趣的:(Redis)