我只想在调用控制器方法时传递我的实体对象。我该怎么做才能将数据插入我的数据库?
RegisterDAO.java
import com.samy.springmvc.beans.RegisterBean;
public interface RegisterDAO {
void createUser(RegisterBean registerbean);
}
RegisterDAOImpl.java
@Autowired
private SessionFactory sessionFactory;
public void createUser(RegisterBean regbean) {
// TODO Auto-generated method stub
Session session = sessionFactory.getCurrentSession();
Register register= new Register();
register.setId(regbean.getId());
register.setUsername(regbean.getUser());
register.setFirstname(regbean.getFirstname());
register.setLastname(regbean.getLastname());
register.setEmail(regbean.getEmail());
register.setAddress(regbean.getAddress());
register.setPhone(regbean.getPhonenumber());
register.setPassword(regbean.getPassword());
session.save(register);
}
registerationController.java
@Autowired
RegisterDAO registerDAO;
@RequestMapping(value="/login",method=RequestMethod.POST)
public ModelAndView returnSuccess()
{
registerDAO.createUser(new RegisterBean());
return new ModelAndView("login");
}
答案 0 :(得分:0)
您需要将bean指定为控制器的参数。
如果使用表格
@RequestMapping(value="/login",method=RequestMethod.POST)
public ModelAndView returnSuccess(@ModelAttribute RegisterBean regBean)
{
registerDAO.createUser(regBean);
...
如果使用JSON
@RequestMapping(value="/login",method=RequestMethod.POST)
public ModelAndView returnSuccess(@RequestBody RegisterBean regBean)
{
registerDAO.createUser(regBean);
...