一、问题描述
在用SpringMVC整合mybatis的时候,在controller中调用service进行保存数据的操作,遇到了Failed to convert from type java.lang.String to type java.util.Date for value………这个错误,然后发现数据库有个字段在Entity中的类型是Date的,然后表单传过来的数据时String类型的,这时就会出现这样的问题,类型转换错误!
二、解决办法
要加一个@InitBinder,查了一下资料,貌似是用于初始化数据的时候,进行数据类型转换,吧String类型转为Date类型,这样就不会报错了。
@InitBinder
public void initBinder(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
本文介绍了解决SpringMVC与MyBatis整合过程中遇到的类型转换错误问题,通过使用@InitBinder注解及自定义日期编辑器实现String到Date类型的正确转换。

被折叠的 条评论
为什么被折叠?



