2013-12-11 72 views
0

如果我已經使用gson將一個類序列化爲包含日期的json類,那麼如何使用json的值使用MyBatis插入到我的h2或MySQL數據庫中?MySQL插入Gson日期

下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我的INSERT語句(使用Spring的@RequestBody構建Bean)服務返回400:錯誤語法。

這裏的MyBatis的映射器的插入語句:

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

當我試圖插入解析JSON日期,事情錯了,但我不知道是什麼。

+0

你試圖parseDateTime一個布爾? – Taylor

+0

對不起,錯字。我已更新。 – Matt

+0

應用程序永遠不會觸及您的插入語句,它無法反序列化您的JSON日期。 –

回答

2

只需使用MySQL DATE_FORMAT功能,讓數據庫爲你做

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