目录
概述
功能:
maven工程demo搭建
目录结构
pom.xml
redis-config.properties
applicationContext-redis.xml
TestValue.java 简单值
TestSet.java set 集合
TestList.java list 集合
TestHash.java map键值对
spring中的框架,用来操作redis,封装了(Jedis,Jredis,RJC);t提供了各种操作;异常处理及序列化,支持发布订阅,并对spring3.1 cache进行了实现;
4.0.0
cn.bufanli
spring-data-redis-demo
1.0-SNAPSHOT
4.3.18.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
redis.clients
jedis
2.8.2
org.springframework.data
spring-data-redis
1.7.11.RELEASE
junit
junit
4.12
test
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* k v
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetValue(){
redisTemplate.boundValueOps("name").set("bufanli");
}
/**
* 取值
*/
@Test
public void testGetValue(){
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
//控制台打印 bufanli
}
/**
* 删除
*/
@Test
public void testDeleteValue(){
redisTemplate.delete("name");
}
}
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Set;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
/**
* 往set集合中存值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameSet").add("曹操");
redisTemplate.boundSetOps("nameSet").add("刘备");
redisTemplate.boundSetOps("nameSet").add("孙权");
}
/**
* 从set集合中获取值
*/
@Test
public void getValue(){
Set nameSet = redisTemplate.boundSetOps("nameSet").members();
System.out.println(nameSet);
}
/**
* 从set集合中获取值
*/
@Test
public void removeValue(){
//删除其中一个
Long remove = redisTemplate.boundSetOps("nameSet").remove("刘备");
Set nameSet = redisTemplate.boundSetOps("nameSet").members();
//打印[曹操, 孙权]
System.out.println(nameSet);
//删除全部
redisTemplate.delete("nameSet");
Set nameSet2 = redisTemplate.boundSetOps("nameSet").members();
//[]
System.out.println(nameSet2);
}
}
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**
* 往list集合中存值
* 右压栈,后添加的对象排在后面
*/
@Test
public void setValue1(){
redisTemplate.boundListOps("nameList1").rightPush("刘备");
redisTemplate.boundListOps("nameList1").rightPush("关羽");
redisTemplate.boundListOps("nameList1").rightPush("张飞");
}
/**
* 往list集合中存值
* 左压栈,后添加的对象排在前面
*/
@Test
public void setValue2(){
redisTemplate.boundListOps("nameList2").leftPush("刘备");
redisTemplate.boundListOps("nameList2").leftPush("关羽");
redisTemplate.boundListOps("nameList2").leftPush("张飞");
}
/**
* 查询方法
*/
@Test
public void getValue(){
//range(开始下标,结束下标);
List nameList1 = redisTemplate.boundListOps("nameList1").range(0, 10);
//右压栈 打印 [刘备, 关羽, 张飞]
System.out.println(nameList1);
List nameList2 = redisTemplate.boundListOps("nameList2").range(0, 10);
//左压栈 打印 [张飞, 关羽, 刘备]
System.out.println(nameList2);
}
@Test
public void searchByIndex(){
//查询第二个
Object nameList2 = redisTemplate.boundListOps("nameList2").index(1);
System.out.println(nameList2);
}
/**
* 删除
*/
public void deleteValue(){
//remove(移除的个数,移除的value); 如果有两个要移除的value移除个数是1 那么只是删除一个,删除的是靠前的
redisTemplate.boundListOps("nameList2").remove(1,"张飞");
//删除全部
redisTemplate.delete("nameList2");
}
}
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Set;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存键值对
*/
@Test
public void setValue(){
redisTemplate.boundHashOps("nameHash").put("a","唐僧");
redisTemplate.boundHashOps("nameHash").put("b","悟空");
redisTemplate.boundHashOps("nameHash").put("c","八戒");
redisTemplate.boundHashOps("nameHash").put("d","沙僧");
}
/**
* 取键值对
*/
@Test
public void getValue(){
//获得所有的key
Set nameHash = redisTemplate.boundHashOps("nameHash").keys();
//打印: [a, b, c, d]
System.out.println(nameHash);
//获取所有value
List nameHash1 = redisTemplate.boundHashOps("nameHash").values();
//打印: [唐僧, 悟空, 八戒, 沙僧]
System.out.println(nameHash1);
//根据key获取值
Object o = redisTemplate.boundHashOps("nameHash").get("a");
//打印: 唐僧
System.out.println(o);
//移除某个key的值
redisTemplate.boundHashOps("nameHash").delete("c");
//打印: [唐僧, 悟空, 沙僧]
System.out.println(redisTemplate.boundHashOps("nameHash").values());
}
}