https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/ Spring Data Redis官方文档
我们首先启动redis,至于怎么安装和怎么启动redis,请自行百度。
这里提一点:启动redis记得把防火墙关闭,或者在防火墙的配置文件中将redis服务的端口打开,否则会出现连接异常。
前期工作准备好之后,我们想要用Spring Data Redis实现对于redis数据的存取数据。首先需要配置我们的配置文件(连接redis的数据可以单独创建一个文件,然后使用el表达式放进配置文件中)
配置文件: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"
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">
<!-- 配置读取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连接工厂:创建Jedis对象的工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- IP地址 -->
<property name="hostName" value="${redis.hostname}"/>
<!-- 端口 -->
<property name="port" value="${redis.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>
第一种:我们先试用默认序列化器进行操作,由配置文件可知,默认序列化器是String类型的
package com.zxw.test;
import com.zxw.pojo.Users;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringRedisTest {
//注入RedisTemplate(redis模板对象)
@Autowired
private RedisTemplate<String,Object> template;
/**
* 添加键值对
*/
@Test
public void insertTest(){
this.template.opsForValue().set("zxw","YoonA");
}
/**
* 获取键值对
*/
@Test
public void selTest(){
String zxw = (String) this.template.opsForValue().get("zxw");
System.out.println(zxw);
}
}
第二种:使用JDK序列化器存储实体对象
package com.zxw.test;
import com.zxw.pojo.Users;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringRedisTest {
@Autowired
private RedisTemplate<String,Object> template;
/**
* 添加对象到redis
* JdkSerializationRedisSerializer 占用redis内存较大(5倍以上,和json对比)
*/
@Test
public void insertUsersTest(){
Users user=new Users();
user.setUsersid(1);
user.setUsersname("Jessica");
user.setUsersage(30);
//更换序列化器
this.template.setValueSerializer(new JdkSerializationRedisSerializer());
this.template.opsForValue().set("user1",user);
}
/**
* 读取实体类从redis
* JdkSerializationRedisSerializer
*/
@Test
public void selUsersTest(){
//更换序列化器
this.template.setValueSerializer(new JdkSerializationRedisSerializer());
Users user1 = (Users) this.template.opsForValue().get("user1");
System.out.println(user1);
}
}
第三种:使用json格式存储实体对象
package com.zxw.test;
import com.zxw.pojo.Users;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringRedisTest {
@Autowired
private RedisTemplate<String,Object> template;
/**
* 添加对象到redis
* json格式
*/
@Test
public void insertUsersJsonTest(){
Users user=new Users();
user.setUsersid(2);
user.setUsersname("Krystal");
user.setUsersage(25);
//更换序列化器
this.template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
this.template.opsForValue().set("user2",user);
}
/**
* 读取实体类从redis
* JdkSerializationRedisSerializer
*/
@Test
public void selUsersJsonTest(){
//更换序列化器
this.template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
Users user2 = (Users) this.template.opsForValue().get("user2");
System.out.println(user2);
}
}
最后我们看下我们的操作有没有成功
这里就截取其中一个显示,可以看到我们有3个键值对,点开user1正是我们使用JDK序列化器存储的数据