导入依赖
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
单机版的配置
创建redis-config.properties
/resources/redis-config.properties
配置连接redis的url和端口号
redis.host=192.168.12.131
redis.port=6379
创建applicationContext-redis.xml
/resources/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: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">
<!-- ################ 配置Redis单机版 #################-->
<!-- 加载属性文件 -->
<context:property-placeholder
location="classpath:redis-config.properties"/>
<!-- 配置连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
运行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class Redis01Test {
@Autowired //注入redis
private RedisTemplate<Object,Object> redisTemplate;
//TODO...
}
String类型数据
boundValueOps(键).set(值)
@Test
public void stringTest(){
/** 设置值name=osc */
redisTemplate.boundValueOps("name").set("osc");
/** 获取值 */
String str = (String) redisTemplate.boundValueOps("name").get();
/** 删除值 */
redisTemplate.delete("name");
}
Set类型数据
@Test
public void setTest(){
/** 设置值 */
redisTemplate.boundSetOps("name").add("osc");
redisTemplate.boundSetOps("name").add("osc","zzy");
/** 获取值 */
Set members = redisTemplate.boundSetOps("name").members();
/** 删除集合中的某一个值 */
redisTemplate.boundSetOps("name").remove("osc");
/** 删除整个集合 */
redisTemplate.delete("name");
}
List类型数据
@Test
public void listTest(){
/** 右压栈:后添加的元素排在后面[...,'osc','zzy'] */
redisTemplate.boundListOps("name").rightPush("osc");
redisTemplate.boundListOps("name").rightPush("zzy");
/** 左压栈:后添加的元素排在前面['zzy','osc',...] */
redisTemplate.boundListOps("name").leftPush("osc");
redisTemplate.boundListOps("name").leftPush("zzy");
/** 获取值(全部) ,0代表开始的索引,-1表示结束的标志 */
List list = redisTemplate.boundListOps("name").range(0, -1);
/** 获取集合中某个元素 */
String s = (String) redisTemplate.boundListOps("name").index(1);
/** 删除集合中某个元素 第一个参数零:代表把匹配到的’osc’全部删除*/
redisTemplate.boundListOps("name").remove(0, "osc");
}
Hash类型数据
@Test
public void hashTest(){
/** 设置值 */
redisTemplate.boundHashOps("name").put("a", "osc");
redisTemplate.boundHashOps("name").put("b", "zzy");
/** 获取所有的key */
Set keys = redisTemplate.boundHashOps("name").keys();
/** 获取所有的value */
List values = redisTemplate.boundHashOps("name").values();
/** 获取指定key的value */
Object obj = redisTemplate.boundHashOps("name").get("b");
/** 删除指定的key与value */
redisTemplate.boundHashOps("name").delete("a");
}
集群版的配置
创建redis-cluster.properties
/resources/redis-cluster.properties
配置连接redis的url:端口号,多台同逗号分隔开
spring.redis.cluster.nodes=192.168.12.131:7001,192.168.12.131:7002,192.168.12.131:7003
创建applicationContext-redis-cluster.xml
/resources/applicationContext-redis-cluster.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: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">
<!-- ################ 配置Redis集群版 #################-->
<!-- 配置资源属性文件源 -->
<bean id="propertySource" class="org.springframework.core.io.support.ResourcePropertySource">
<constructor-arg value="redis-cluster.properties"/>
</bean>
<!-- 配置Redis集群节点信息 -->
<bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<constructor-arg ref="propertySource"/>
</bean>
<!-- 配置Jedis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="clusterConfig"/>
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
运行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis-cluster.xml"})
public class Redis02Test {
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
......
}