使用RedisTemplate往redis存入java实体对象

导入依赖

<!-- 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());
	}
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值