介绍
SpringDataRedis是Spring大家族中的一个成员,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
创建一个JavaProject ,导入如下jar
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.1.1.jar
commons-pool2-2.3.jar
jedis-2.7.2.jar
spring-aop-4.2.0.RELEASE.jar
spring-aspects-4.2.0.RELEASE.jar
spring-beans-4.2.0.RELEASE.jar
spring-context-4.2.0.RELEASE.jar
spring-core-4.2.0.RELEASE.jar
spring-data-redis-1.6.0.RELEASE.jar
spring-expression-4.2.0.RELEASE.jar
spring-test-4.2.0.RELEASE.jar
spring-tx-4.2.0.RELEASE.jar
jackson-annotations-2.8.0.jar
jackson-core-2.8.10.jar
jackson-databind-2.8.10.jar
创建redis配置文件 applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置读取properties文件的工具类 -->
<context:property-placeholder location="classpath:redis.properties"/>
<!-- Jedis连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
</bean>
<!-- Jedis的连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.conn.hostName}"/>
<property name="port" value="${redis.conn.port}"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- Redis模板对象 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<!-- 序列化器:能够把我们储存的key与value做序列化处理的对象 -->
<!-- 配置默认的序列化器 -->
<!-- keySerializer、valueSerializer 配置Redis中的String类型key与value的序列化器 -->
<!-- HashKeySerializer、HashValueSerializer 配置Redis中的Hash类型key与value的序列化器 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
</beans>
redis.properties
redis.pool.maxTotal=20
redis.pool.maxIdle=10
redis.pool.minIdle=5
#redis所在虚拟机ip。端口
redis.conn.hostName=192.168.179.131
redis.conn.port=6379
注 :这两个配置文件都在src目录下
需要实现序列化 ,因为要以json类型以字节的形式传输到redis所在虚拟机
package ah.szxy.pojo;
import java.io.Serializable;
public class Users implements Serializable{
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
存储对象时 ,更换JdkSerializationRedisSerializer序列化器 用于将对象以字节的形式存储到redis数据库中 . 序列化后的数据比使用工具包更占用redis内存 序列化和反序列化需要用同一个序列化器
package ah.szxy.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.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import ah.szxy.pojo.Users;
/**
* Redis测试
*
* @author Administrator
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class RedisTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 添加键值对
*/
@Test
public void test1() {
this.redisTemplate.opsForValue().set("key", "test");
}
/**
* 获取redis中的数据
*/
@Test
public void test2() {
String str = (String) this.redisTemplate.opsForValue().get("key");
System.out.println(str);
}
/**
* 添加Users
*
* 存储对象时 ,更换JdkSerializationRedisSerializer序列化器 用于将对象以字节的形式存储到redis数据库中
* 序列化后的数据比使用工具包更占用redis内存 序列化和反序列化需要用同一个序列化器
*/
@Test
public void test3() {
Users users = new Users();
users.setAge(30);
users.setId(1);
users.setName("张三");
// 更换序列化器
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("users", users);
}
/**
* 获取Users
*
*/
@Test
public void test4() {
// 更换序列化器
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
Users users = (Users) this.redisTemplate.opsForValue().get("users");
System.out.println(users);
}
/**
* 添加Users json格式
*/
@Test
public void test5() {
Users users = new Users();
users.setAge(24);
users.setId(2);
users.setName("间桐樱");
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
this.redisTemplate.opsForValue().set("UsersJson", users);
}
/**
* 获取Users json格式
*/
@Test
public void test6() {
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
Users users = (Users) this.redisTemplate.opsForValue().get("UsersJson");
System.out.println(users);
}
}
引用RedisTemplate后 ,可以设置的序列化器如下
重要的几个功能如下
注 : 本例中使用的 Jackson2JsonRedisSerializer序列化器和 JacksonJsonRedisSerializer序列化器功能一样
链接:https://pan.baidu.com/s/1G_pxYPvCUmvcFToZOwwoYw
提取码:wqxb