SpringDataRedis 入門demo (String,list,set,hashset)
阿新 • • 發佈:2018-11-05
專案結構:
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");
}
}