版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/83388945
项目结构:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lucifer</groupId>
<artifactId>springDataRedisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.10.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
redisConfig.properties: redis的配置文件
redis.host=192.168.59.133
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
applicationContext-redis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
testValue:测试类
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 redis.clients.jedis.Jedis;
/**
* @author: Lucifer
* @create: 2018-10-25 22:25
* @description:
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
/**
* 设置key,value
*/
@Test
public void setValue() {
redisTemplate.boundValueOps("name").set("lucifer");
}
/**
* 通过key获取value
*/
@Test
public void getValue() {
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
/**
* 通过key所有value
*/
@Test
public void deleteValue() {
redisTemplate.delete("name");
}
}
TestSet:测试类
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: Lucifer
* @create: 2018-10-25 23:56
* @description:
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
RedisTemplate redisTemplate;
/**
* 设置key,value
* 以set方式
*/
@Test
public void setValue() {
redisTemplate.boundSetOps("nameset").add("老王");
redisTemplate.boundSetOps("nameset").add("老张");
redisTemplate.boundSetOps("nameset").add("老李");
}
/**
* 通过key获取value,value值不按顺序排列
*/
@Test
public void getValue() {
final Set name = redisTemplate.boundSetOps("nameset").members();
System.out.println(name);
}
/**
* 删除单个value
*/
@Test
public void removeValue() {
redisTemplate.boundSetOps("nameset").remove("老李");
}
/**
* 删除所有此key的value
*/
@Test
public void delete() {
redisTemplate.delete("nameset");
}
}
TestList :测试类
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: Lucifer
* @create: 2018-10-26 00:19
* @description:
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
RedisTemplate redisTemplate;
/**
* 右压栈:后添加的元素排在后面
*/
@Test
public void testSetValue() {
redisTemplate.boundListOps("namelist").rightPush("孙悟空");
redisTemplate.boundListOps("namelist").rightPush("猪八戒");
redisTemplate.boundListOps("namelist").rightPush("沙僧");
redisTemplate.boundListOps("namelist").rightPush("唐僧");
}
@Test
public void testGetValue() {
final List name = redisTemplate.boundListOps("namelist").range(0, 10);
System.out.println(name);
}
/**
* 左压栈:后添加的元素排在前面
*/
@Test
public void testValue2() {
redisTemplate.boundListOps("namelist2").leftPush("孙悟空");
redisTemplate.boundListOps("namelist2").leftPush("猪八戒");
redisTemplate.boundListOps("namelist2").leftPush("沙僧");
redisTemplate.boundListOps("namelist2").leftPush("唐僧");
}
@Test
public void testGetValu2() {
final List name = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(name);
}
/**
* 按索引位置查询
*/
@Test
public void searchByIndex() {
final Object name2 = redisTemplate.boundListOps("namelist2").index(1);
System.out.println(name2);
}
}
TestHash:测试类
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: Lucifer
* @create: 2018-10-26 00:44
* @description:
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetValue(){
redisTemplate.boundHashOps("namehash").put("a","小A");
redisTemplate.boundHashOps("namehash").put("b","小B");
redisTemplate.boundHashOps("namehash").put("c","小C");
redisTemplate.boundHashOps("namehash").put("d","小D");
}
/**
* 获取所有的key
*/
@Test
public void testGetValue(){
final Set name = redisTemplate.boundHashOps("namehash").keys();
}
/**
* 获取所有value
*/
public void testGetValues(){
final List name = redisTemplate.boundHashOps("namehash").values();
System.out.println(name);
}
/**
* 获取key取值
*/
@Test
public void searchValueByKey(){
redisTemplate.boundHashOps("namehash").get("b");
}
/**
* 通过key删除
*/
@Test
public void removeValue(){
redisTemplate.boundHashOps("namehash").delete("c");
}
}