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

导入依赖


	
	org.springframework
	spring-core
	4.3.2.RELEASE


	
		redis.clients
		jedis
		2.9.1
	

	
		org.springframework.data
		spring-data-redis
		1.7.2.RELEASE
	

这里要尤其注重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




	Jedis Configuration

    
	
	
		 
		 
		 
	
	
    
        
        
         
       
        
    

    
     
     
     
     
     
     
     

测试类

//测试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());
	}

你可能感兴趣的:(java案例)