pom依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.0</version> </dependency>
代码直接可用:
import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import redis.clients.jedis.Jedis; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class TestRedis { private Jedis jedis; @BeforeClass public void setup() { //连接redis服务器,ip jedis = new Jedis("ip", 6379); //权限认证 jedis.auth("123456"); } /** * 删除数据 * * * @param key:要删除数据的key * @return:返回boolean值,表示是否删除成功 */ public boolean delete(String key) { if (jedis.exists(key)) { if (jedis.del(key) == 1) { System.out.println("删除数据成功"); return true; } else { System.out.println("删除数据失败"); return false; } } else { System.out.println(key + "不存在"); return false; } } @Test public void test() { delete("age"); } }