mybatis中string和date的轉(zhuǎn)換方式
實(shí)體里用的java.util.date,數(shù)據(jù)庫用的是datetime,頁面是字符串<input type="date">。將頁面標(biāo)簽<input type="date">的內(nèi)容添加到數(shù)據(jù)庫
實(shí)體
public class BaseInformation {
//信息主鍵
private String id;
//信息標(biāo)題
private String title;
//信息類型id(需要在數(shù)據(jù)字典定義)
private String typeCode;
//屬性id(需要在數(shù)據(jù)字典定義)
private String propertityId;
//信息可視范圍,部門可見或者整個(gè)單位可見,值是組織結(jié)構(gòu)的id
private String scope;
//內(nèi)容
private String content;
//發(fā)布人id
private String releaseId;
//是否發(fā)布,1發(fā)布,0保存
private Integer released;
//接收人id,對(duì)應(yīng)tb_base_information_receiver的主鍵
private String receiver;
//創(chuàng)建時(shí)間就是發(fā)布時(shí)間
private Date createDate;
//更新時(shí)間
private Date updateDate;
//開始時(shí)間
private Date beginDate;
//結(jié)束時(shí)間
private Date endDate;
//定時(shí)器,規(guī)定什么時(shí)候發(fā)布信息
private Date timer;
。。。。。省略getter和setter
表
CREATE TABLE `tb_base_information` ( `id` varchar(48) NOT NULL, `title` varchar(128) DEFAULT NULL COMMENT '標(biāo)題', `type_code` varchar(48) DEFAULT NULL COMMENT '類型id(需要在數(shù)據(jù)字典定義),公告、新聞等', `propertity_id` varchar(48) DEFAULT NULL COMMENT '屬性id(需要在數(shù)據(jù)字典定義)', `scope` varchar(255) DEFAULT NULL COMMENT '信息可視范圍,部門可見或者整個(gè)單位可見,值是組織結(jié)構(gòu)的id', `content` text COMMENT '內(nèi)容', `release_id` varchar(48) DEFAULT NULL COMMENT '發(fā)布人id', `released` int(11) DEFAULT NULL COMMENT '是否發(fā)送,1發(fā)送0保存為草稿', `create_date` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', `update_date` datetime DEFAULT NULL COMMENT '更新時(shí)間', `begin_date` datetime DEFAULT NULL COMMENT '信息有效期的起始時(shí)間', `end_date` datetime DEFAULT NULL COMMENT '信息有效期的結(jié)束時(shí)間', `timer` datetime DEFAULT NULL COMMENT '定時(shí)器,指定發(fā)送信息的時(shí)間', `expiry_date` datetime DEFAULT NULL COMMENT '是否永久有效,1是0否', `to_top` int(1) DEFAULT NULL COMMENT '信息是否置頂,1置頂,0否', `mark_star` int(1) DEFAULT NULL COMMENT '做星標(biāo)標(biāo)記用,1在星標(biāo)公告里顯示,0否', `receiver` varchar(48) DEFAULT NULL COMMENT '接收人id,對(duì)應(yīng)tb_base_information_receiver的主鍵', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='信息發(fā)布表';
頁面

<form name="form" method="post" action="/information/test" enctype="multipart/form-data">
類型:<input type="text" name="typeCode" ><br/>
標(biāo)題:<input type="text" name="title" ><br/>
內(nèi)容:<textarea name="content" cols="30" rows="10"></textarea><br/>
創(chuàng)建時(shí)間:<input type="date" name="createDate"/><br/>
更新時(shí)間:<input type="date" name="updateDate"/><br/>
有效期開始時(shí)間:<input type="date" name="beginDate"/><br/>
有效期結(jié)束時(shí)間:<input type="date" name="endDate"/><br/>
定時(shí)器:<input type="date" name="timer"/><br/>
<input type="submit" value="提交">
</form>
controller
@RequestMapping("/test")
public String test(BaseInformation baseInformation, HttpServletRequest request) throws Exception {
String id = UUID.randomUUID().toString();
baseInformation.setId(id);
//baseInformation.setId(UUID.randomUUID().toString().replaceAll("-",""));
System.out.println("request.getParameter(createDate)" + request.getParameter("createDate"));
Date createDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("createDate"));
Date updateDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("createDate"));
Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("beginDate"));
Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endDate"));
Date timer = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("timer"));
baseInformation.setCreateDate(createDate);
baseInformation.setUpdateDate(updateDate);
baseInformation.setBeginDate(beginDate);
baseInformation.setEndDate(endDate);
baseInformation.setTimer(timer);
service.save(baseInformation);
return "information/test";
}

運(yùn)行結(jié)果

Field error in object 'baseInformation' on field 'createDate': rejected value [2018-11-08]; codes [typeMismatch.baseInformation.createDate,typeMismatch.createDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [baseInformation.createDate,createDate]; arguments []; default message [createDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2018-11-08'; nested exception is java.lang.IllegalArgumentException]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:117)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
修改方法
修改實(shí)體
string和date的類型轉(zhuǎn)換失敗,此時(shí)在實(shí)體中,對(duì)需要進(jìn)行轉(zhuǎn)換的類型添加如下注解,實(shí)現(xiàn)java.lang.String和java.util.Date之間自動(dòng)轉(zhuǎn)換
@DateTimeFormat(pattern="yyyy-MM-dd")//頁面寫入數(shù)據(jù)庫時(shí)格式化 @JSONField(format="yyyy-MM-dd")//數(shù)據(jù)庫導(dǎo)出頁面時(shí)json格式化
即

修改contoller
既然java.lang.String和java.util.Date之間可以自動(dòng)轉(zhuǎn)換了,后臺(tái)就不需要通過request獲取參數(shù)來進(jìn)行轉(zhuǎn)換,可以將紅方格中的注釋掉

依賴添加
1.注解@JsonFormat
<!--JsonFormat-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
2.注解@DateTimeFormat
@DateTimeFormat的使用和@jsonFormat差不多,首先需要引入是spring還有jodatime,spring我就不貼了
<!-- joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
關(guān)于@DateTimeFormat和@JsonFormat還可以參考http://www.dhdzp.com/article/176268.htm
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 線程的生命周期詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了Java 線程的生命周期的相關(guān)資料,并附簡單實(shí)例代碼,幫助大家理解,需要的朋友可以參考下2016-10-10
Intellij Idea部署OpenCV 4.0.0環(huán)境
這篇文章主要為大家詳細(xì)介紹了Intellij Idea部署OpenCV 4.0.0環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
SpringBoot實(shí)現(xiàn)自動(dòng)配置的示例代碼
本文主要介紹了SpringBoot實(shí)現(xiàn)自動(dòng)配置的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
解析Java中所有錯(cuò)誤和異常的父類java.lang.Throwable
這篇文章主要介紹了Java中所有錯(cuò)誤和異常的父類java.lang.Throwable,文章中簡單地分析了其源碼,說明在代碼注釋中,需要的朋友可以參考下2016-03-03
如何使用Spring Cloud Feign日志查看請(qǐng)求響應(yīng)
這篇文章主要介紹了如何使用Spring Cloud Feign日志查看請(qǐng)求響應(yīng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
IntelliJ IDEA使用tomcat和jetty配置詳解
這篇文章主要介紹了IntelliJ IDEA使用tomcat和jetty配置詳解,以便進(jìn)一步地開發(fā)和調(diào)試,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
springboot使用kafka推送數(shù)據(jù)到服務(wù)端的操作方法帶認(rèn)證
在使用Kafka進(jìn)行數(shù)據(jù)推送時(shí),遇到認(rèn)證問題導(dǎo)致連接失敗,本文詳細(xì)介紹了Kafka的認(rèn)證配置過程,包括配置文件的引入和參數(shù)設(shè)置,實(shí)際測(cè)試表明,需要正確配置sasl.jaas.config以及其他認(rèn)證參數(shù),探討了配置文件是否可以同時(shí)存在多個(gè)配置塊的可能性,并提出了實(shí)際操作中的注意事項(xiàng)2024-11-11
Java?Chassis3應(yīng)用視角的配置管理技術(shù)解密
這篇文章主要為大家介紹了Java?Chassis3應(yīng)用視角的配置管理相關(guān)的機(jī)制和背后故事,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

