MySQL Insert Gson Date

你说的曾经没有我的故事 提交于 2019-12-11 14:58:25

问题


If I have serialized a class into json, which contains dates, using gson, how can I use the value of the json to insert into my h2 or MySQL database using MyBatis?

The json below is constructed using the following code:

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);

Which makes json:

{
   "isPublished":true,
   "publicationDate":"2013-55-10-09:55:07"
}

But when I pass this json to my insert statement (using spring's @RequestBody to construct the Bean) the service returns a 400: Bad Syntax.

Here's the MyBatis Mapper's insert statement:

INSERT INTO beans (isPublished, publicationDate)
VALUES (#{isPublished}, parseDateTime(#{publicationDate}, 'yyyy-mm-dd-hh:mm:ss');

When I'm trying to insert the parsed json date, something is going wrong, but I'm not sure what.


回答1:


Just use the Mysql date_format function and let the database do it for you

 date_format( #{publicationDate, jdbcType=VARCHAR}, '%Y-%m-%d-%H-%i-%s' )


来源:https://stackoverflow.com/questions/20509883/mysql-insert-gson-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!