Section 1 redis components: 4, installation (omitted); 5, data type (abbreviated); 6, javaAPI operation;

The third step: redis operation of javaAPI

Operating string data type

/ **
 * 
Add string type data
 * /
@Test
public void  addstr () {
    Jedis Resource =  jedisPool .getResource ();
    // add
    
resource.set ( "jediskey""jedisvalue" );
    // Query
    
String jediskey = resource .get ( "jediskey" );
    System. OUT .println (jediskey);
    // modify
    
resource.set ( "jediskey" , "jedisvalueUpdate" );
    // delete
    
resource.del ( "jediskey" );
    // achieve integer growth of operating data
    
resource.incr ("jincr");
    resource.incrBy("jincr",3);
    String jincr = resource.get("jincr");
    System.out.println(jincr);
    resource.close();
}

 

Operation hash list data type

/ **
 * 
Operation hash type data
 * /
@Test
public void  hashOperate () {
    Jedis Resource =  jedisPool .getResource ();
    // add data
    
resource.hset ( "jhsetkey" , "jmapkey" , "jmapvalue" );
    Resource. HSET ( "jhsetkey" , "jmapkey2" , "jmapvalue2" );
    // get all the data
    
the Map <String, String> jhsetkey = resource.hgetAll ( "jhsetkey" );
    for  (String S: jhsetkey.keySet ()) {
        the System . OUT .println (S);
    }
    //修改数据
    
resource.hset("jhsetkey","jmapkey2","jmapvalueupdate2");
    Map<String, String> jhsetkey2 = resource.hgetAll("jhsetkey");
    for (String s : jhsetkey2.keySet()) {
        System.out.println("修改数据打印"+s);
    }
    //删除数据
    
resource.del("jhsetkey");

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

 

Operation list type data

 

/ **
 * 
operation list type of data
 * /
@Test
public void  listOperate () {
    Jedis Resource =  jedisPool .getResource ();
    // insert elements from the left
    
resource.lpush ( "listKey" , "listvalue1" , "listvalue1" , "listvalue2" );

    // element is removed from the right
    
resource.rpop ( "listKey" );
    // get values for all
    
List <String> listKey = resource.lrange ( "listKey" , 0, -1);
    for  (S String : listKey) {
        the System. OUT .println (S);
    }
    resource.close();
}

 

Operation data set type

/ **
 * SET
type of data manipulation
 * /
@Test
public void  setOperate () {
    Jedis Resource =  jedisPool .getResource ();
    // add data
    
resource.sadd ( "the setkey""setvalue1""setvalue1""setvalue2""setvalue3" );
    // query data
    
the Set <String> = resource.smembers the setkey ( "the setkey" );
    for  (S String: the setkey) {
        . the System OUT .println (S);
    }
    // remove out a data
    
resource.srem ( "setkey" ,"setvalue3");
    resource.close();
}

 

Guess you like

Origin www.cnblogs.com/mediocreWorld/p/11456564.html