导入依赖
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
这里要尤其注重sping和spring-data-redisjar版本的问题,我也在这入了坑,用如上版本即可解决
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1464328592089679810L;
private String name;
private String passowrd;
private int age;
private double money;
省略setget
注意要实现Serializable 接口
注意修改密码
spring-context-jedis.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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-lazy-init="true">
<description>Jedis Configuration</description>
<!-- 加载配置属性文件 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" /> <!-- 最大能够保持idel状态的对象数 -->
<property name="maxTotal" value="60000" /> <!-- 最大分配的对象数 -->
<property name="testOnBorrow" value="true" /> <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="password" value="123456" />
<!-- <property name="database" value="${redis.dbIndex}" />-->
<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer
"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<property name="keySerializer" ref="stringRedisSerializer"></property>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
</bean>
</beans>
测试类
//测试redis保存java对象
@Test
public void testRedisTemplate()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context-jedis.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
User user = new User();
user.setName("哈哈");
user.setPassowrd("1234");
user.setAge(23);
user.setMoney(234.234);
redisTemplate.opsForValue().set("user1", user);
User user2 = (User) redisTemplate.opsForValue().get("user1");
System.out.println(user2.toString());
}