這篇文章給大家分享的是有關mybatis中string和date如何轉換的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
實體里用的java.util.date,數據庫用的是datetime,頁面是字符串<input type="date">。將頁面標簽<input type="date">的內容添加到數據庫
public class BaseInformation {
//信息主鍵
private String id;
//信息標題
private String title;
//信息類型id(需要在數據字典定義)
private String typeCode;
//屬性id(需要在數據字典定義)
private String propertityId;
//信息可視范圍,部門可見或者整個單位可見,值是組織結構的id
private String scope;
//內容
private String content;
//發布人id
private String releaseId;
//是否發布,1發布,0保存
private Integer released;
//接收人id,對應tb_base_information_receiver的主鍵
private String receiver;
//創建時間就是發布時間
private Date createDate;
//更新時間
private Date updateDate;
//開始時間
private Date beginDate;
//結束時間
private Date endDate;
//定時器,規定什么時候發布信息
private Date timer;。。。。。省略getter和setter
CREATE TABLE `tb_base_information` ( `id` varchar(48) NOT NULL, `title` varchar(128) DEFAULT NULL COMMENT '標題', `type_code` varchar(48) DEFAULT NULL COMMENT '類型id(需要在數據字典定義),公告、新聞等', `propertity_id` varchar(48) DEFAULT NULL COMMENT '屬性id(需要在數據字典定義)', `scope` varchar(255) DEFAULT NULL COMMENT '信息可視范圍,部門可見或者整個單位可見,值是組織結構的id', `content` text COMMENT '內容', `release_id` varchar(48) DEFAULT NULL COMMENT '發布人id', `released` int(11) DEFAULT NULL COMMENT '是否發送,1發送0保存為草稿', `create_date` datetime DEFAULT NULL COMMENT '創建時間', `update_date` datetime DEFAULT NULL COMMENT '更新時間', `begin_date` datetime DEFAULT NULL COMMENT '信息有效期的起始時間', `end_date` datetime DEFAULT NULL COMMENT '信息有效期的結束時間', `timer` datetime DEFAULT NULL COMMENT '定時器,指定發送信息的時間', `expiry_date` datetime DEFAULT NULL COMMENT '是否永久有效,1是0否', `to_top` int(1) DEFAULT NULL COMMENT '信息是否置頂,1置頂,0否', `mark_star` int(1) DEFAULT NULL COMMENT '做星標標記用,1在星標公告里顯示,0否', `receiver` varchar(48) DEFAULT NULL COMMENT '接收人id,對應tb_base_information_receiver的主鍵', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='信息發布表';

<form name="form" method="post" action="/information/test" enctype="multipart/form-data"> 類型:<input type="text" name="typeCode" ><br/> 標題:<input type="text" name="title" ><br/> 內容:<textarea name="content" cols="30" rows="10"></textarea><br/> 創建時間:<input type="date" name="createDate"/><br/> 更新時間:<input type="date" name="updateDate"/><br/> 有效期開始時間:<input type="date" name="beginDate"/><br/> 有效期結束時間:<input type="date" name="endDate"/><br/> 定時器:<input type="date" name="timer"/><br/> <input type="submit" value="提交"> </form>
@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";
}

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)
string和date的類型轉換失敗,此時在實體中,對需要進行轉換的類型添加如下注解,實現java.lang.String和java.util.Date之間自動轉換
@DateTimeFormat(pattern="yyyy-MM-dd")//頁面寫入數據庫時格式化 @JSONField(format="yyyy-MM-dd")//數據庫導出頁面時json格式化
即

既然java.lang.String和java.util.Date之間可以自動轉換了,后臺就不需要通過request獲取參數來進行轉換,可以將紅方格中的注釋掉

<!--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>
@DateTimeFormat的使用和@jsonFormat差不多,首先需要引入是spring還有jodatime,spring我就不貼了
<!-- joda-time --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.3</version> </dependency>
感謝各位的閱讀!關于“mybatis中string和date如何轉換”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。