问题
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