package com.example.demo.bean;import java.io.Serializable;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:15. */public class User implements Serializable{ private static final long serialVersionUID = 934073895746700367L; private String id; private String name; private Integer age; public User(Integer age, String id, String name) { super(); this.age = age; this.id = id; this.name = name; } get,set方法省略....}
3.数据访问
package com.example.demo.dao;import com.example.demo.bean.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Repository;import javax.annotation.Resource;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:17. */@Repositorypublic class UserDao { @Autowired StringRedisTemplate stringRedisTemplate; @Resource(name = "stringRedisTemplate") ValueOperations valOpsStr; @Autowired RedisTemplate
4.控制器,用userDao或RedisTemplate存取数据
package com.example.demo.controller;import com.example.demo.bean.User;import com.example.demo.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:26. */@RestControllerpublic class RedisDataController { @Autowired UserDao userDao; @Autowired RedisTemplate redisTemplate; @RequestMapping("/set") public void set(){ User user = new User(32,"1","gg"); userDao.save(user); userDao.stringRedisTemplateDemo(); redisTemplate.opsForValue().set("22",user); } @RequestMapping("/get1") public String getString(){ return userDao.getString(); } @RequestMapping("get2") public User getUser(String id){ return userDao.getUser(id); }}
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.demo.bean.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)