第一步先把junit的3.8.1版本改成高版本
我用的是
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
其次再加上两个jar包。
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
然后在这里建立测试类
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisTest {
@Test
public void test1() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.set("userName", "张三");
System.out.println(jedis.get("userName"));
jedis.close();
}
@Test
public void test2() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.select(1);
jedis.setex("code", 20, "999999");
System.out.println(jedis.get("code"));
jedis.close();
}
@Test
public void test3() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.lpush("mylist", "aa","bb","cc");
jedis.rpush("mylist", "dd","ee","ff");
List<String> jlist= jedis.lrange("mylist", 0, -1);
for (String string : jlist) {
System.out.println(string);
}
jedis.close();
}
@Test
public void test4() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.sadd("myset", "aa","bb","cc");
Set<String> set= jedis.smembers("myset");
for (String string : set) {
System.out.println(string);
}
jedis.close();
}
@Test
public void test5() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.hset("myhash", "username", "张三");
jedis.hset("myhash", "pwd", "123");
Map<String, String>map=jedis.hgetAll("myhash");
Set<String> set=map.keySet();
for (String key : set) {
System.out.println(key+"\t"+map.get(key));
}
jedis.close();
}
@Test
public void test6() {
Jedis jedis=new Jedis("127.0.0.1", 6379);
jedis.zadd("mysort", 0, "a");
jedis.zadd("mysort", 20, "w");
jedis.zadd("mysort", 10, "f");
Set<String> set=jedis.zrange("mysort", 0, -1);
for (String string : set) {
System.out.println(string);
}
jedis.close();
}
@Test
public void test7() {
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10);
JedisPool jedisPool=new JedisPool(config,"127.0.0.1",6379);
Jedis jedis=jedisPool.getResource();
jedis.set("aaaaa", "bbbbb");
jedis.close();
}
}
ok,简单的小练习就完成了。