redis不仅可以通过命令行进行操作,同时redis也可以通过javaAPI进行操作,我们可以通过使用javaAPI来对redis数据库当中的各种数据类型进行操作
第一步:创建maven工程并导入jar包
< dependencies>
< dependency>
< groupId>redis.clients</ groupId>
< artifactId>jedis</ artifactId>
< version>2.9.0</ version>
</ dependency>
< dependency>
< groupId>junit</ groupId>
< artifactId>junit</ artifactId>
< version>4.12</ version>
< scope>test</ scope>
</ dependency>
< dependency>
< groupId>org.testng</ groupId>
< artifactId>testng</ artifactId>
< version>6.14.3</ version>
< scope>test</ scope>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId>org.apache.maven.plugins</ groupId>
< artifactId>maven-compiler-plugin</ artifactId>
< version>3.0</ version>
< configuration>
< source>1.8</ source>
< target>1.8</ target>
< encoding>UTF-8</ encoding>
</ configuration>
</ plugin>
</ plugins>
</ build>
第二步:连接以及关闭redis客户端
private JedisPool jedisPool;
private JedisPoolConfig config;
@BeforeTest
public void redisConnectionPool(){
config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMaxWaitMillis(3000);
config.setMaxTotal(50);
config.setMinIdle(5);
jedisPool = new JedisPool(config, "node01", 6379);
}
@AfterTest
public void closePool(){
jedisPool.close();
}
第三步:redis的javaAPI操作
操作string类型数据
/**
* 添加string类型数据
*/
@Test
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列表类型数据
/**
* 操作hash类型数据
*/
@Test
public void hashOperate(){
Jedis resource = jedisPool.getResource();
//添加数据
resource.hset("jhsetkey","jmapkey","jmapvalue");
resource.hset("jhsetkey","jmapkey2","jmapvalue2");
//获取所有数据
Map<String, String> jhsetkey = resource.hgetAll("jhsetkey");
for (String s : jhsetkey.keySet()) {
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);
}
}
操作list类型数据
/**
* 操作list类型的数据
*/
@Test
public void listOperate(){
Jedis resource = jedisPool.getResource();
//从左边插入元素
resource.lpush("listkey","listvalue1","listvalue1","listvalue2");
//从右边移除元素
resource.rpop("listkey");
//获取所有值
List< String> listkey = resource.lrange("listkey", 0, -1);
for (String s : listkey) {
System.out.println(s);
}
resource.close();
}
操作set类型的数据
/**
* set类型数据操作
*/
@Test
public void setOperate(){
Jedis resource = jedisPool.getResource();
//添加数据
resource.sadd("setkey", "setvalue1", "setvalue1", "setvalue2", "setvalue3");
//查询数据
Set< String> setkey = resource.smembers("setkey");
for (String s : setkey) {
System.out.println(s);
}
//移除掉一个数据
resource.srem("setkey","setvalue3");
resource.close();
}