1.引入jar包
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!--添加spring-datajar包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
2.String类型测试
//测试字符串 IP:6379
@Test
public void testString(){
Jedis jedis = new Jedis("192.168.126.166",6379);
jedis.set("aa","1807班");
System.out.println(jedis.get("aa"));
}
3.hash类型数据测试
//测试hash数据存储
@Test
public void testHash(){
Jedis jedis = new Jedis("192.168.126.166",6379);
jedis.hset("dog", "id", "100");
jedis.hset("dog", "name", "中华田园犬");
jedis.hset("dog", "age", "88");
System.out.println(jedis.hget("dog", "name"));
System.out.println(jedis.hgetAll("dog"));
}
4.List类型测试
//测试List集合
@Test
public void testList(){
Jedis jedis = new Jedis("192.168.126.166",6379);
jedis.lpush("idList", "1","2","3");
System.out.println(jedis.lpop("idList"));
}
5.事务控制
//redis中事务控制
@Test
public void testTx(){
Jedis jedis = new Jedis("192.168.126.166",6379);
Transaction transaction = jedis.multi(); //开启事务.
transaction.set("aa", "aaa");
transaction.set("bb", "bbb");
transaction.exec(); //事务提交
System.out.println("执行成功!!!");
//transaction.discard(); //事务回滚
}