Spring Data Redis
一,准备工作
1.构建maven工程SpringDataRedisDemo
2.引入spring相关依赖,引入junit相关依赖
3.引入jedisheSP
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
在src/main/resources中创建properties文件夹,建立redis-config.properties
#Redis setting
#Server Ip
redis.host=192.168.159.130
#server port
redis.port=6379
#server pass
redis.pass=
#use dbIndex
redis.database=0
#控制一个pool最多有多少个状态为idel(空闲的)jedis实例
redis.maxIdle=300
redis.minIdle=1
redis.maxTotal=30
#表示当borrow(引入)一个jedis实例时,最大的等待空闲时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;
redis.maxWait=3000
#在borrow一个jedis实例中,是否进行validate操作,如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true
在resources下新建一个spring包里面新建一个xml文件application-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://activemq.apache.org/schema/core
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd ">
<context:annotation-config />
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath*:redis-config.properties/" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- jedis连接工厂-->
<bean id="redisConnectionFactory" 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"/>
<!-- redisTemplate类 负责操作redis服务器封装类-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean>
</beans>
新建一个测试类获取redisTemplate实例
package com.gd.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;
/**
* @Author 晨边#CB
* @Date:created in 2019/9/28 8:53
* @Version V1.0
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestRedusTemplate {
@Autowired
RedisTemplate redisTemplate;
//存入键值对
@Test
public void test(){
redisTemplate.boundValueOps("name").set("chenbin");
}
//取出键值对对应的name的值
@Test
public void testGetString(){
String name = (String)redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
//获取存入值的字符长度
@Test
public void lentest(){
Long len = redisTemplate.boundValueOps("name").size();
System.out.println(len);
}
//删除name对应的值
@Test
public void deleteKey(){
redisTemplate.delete("name");
}
}
调用redisTemplate的Set方法:
package com.gd.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 晨边#CB
* @Date:created in 2019/9/28 9:29
* @Version V1.0
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
RedisTemplate redisTemplate;
@Test
public void testSet1(){
redisTemplate.boundSetOps("username").add("chenbin");
redisTemplate.boundSetOps("username").add("xiaoming");
redisTemplate.boundSetOps("username").add("xiaohong");
redisTemplate.boundSetOps("username").add("xiaoqiang");
}
@Test
public void testRemove(){
redisTemplate.boundValueOps("username").rename("chenbin");
}
@Test
public void testSet2(){
Set values = redisTemplate.boundSetOps("username").members();
System.out.println(values);
}
@Test
public void testDelete(){
redisTemplate.delete("username");
}
}