【发布时间】:2013-12-11 03:18:27
【问题描述】:
如果我使用 gson 将一个类序列化为包含日期的 json,我如何使用 json 的值使用 MyBatis 将其插入到我的 h2 或 MySQL 数据库中?
下面的json是使用以下代码构造的:
Bean bean = new Bean();
bean.setIsPublished(true);
bean.setPublicationDate(Calendar.getInstance().getTime());
Gson gson = new GsonBuilder().setDateFormat("yyyy-mm-dd-hh:mm:ss").create();
String json = gson.toJson(bean);
这使得 json:
{
"isPublished":true,
"publicationDate":"2013-55-10-09:55:07"
}
但是当我将此 json 传递给我的插入语句(使用 spring 的 @RequestBody 构造 Bean)时,服务返回 400: Bad Syntax。
这是 MyBatis Mapper 的插入语句:
INSERT INTO beans (isPublished, publicationDate)
VALUES (#{isPublished}, parseDateTime(#{publicationDate}, 'yyyy-mm-dd-hh:mm:ss');
当我尝试插入解析的 json 日期时,出了点问题,但我不确定是什么。
【问题讨论】:
-
您要在布尔值上解析日期时间?
-
对不起,错字。我已经更新了。
-
应用程序永远不会命中您的插入语句,它无法反序列化您的 JSON 日期。