mybatis中string和date的轉(zhuǎn)換方式
實(shí)體里用的java.util.date,數(shù)據(jù)庫(kù)用的是datetime,頁(yè)面是字符串<input type="date">。將頁(yè)面標(biāo)簽<input type="date">的內(nèi)容添加到數(shù)據(jù)庫(kù)
實(shí)體
public class BaseInformation { //信息主鍵 private String id; //信息標(biāo)題 private String title; //信息類型id(需要在數(shù)據(jù)字典定義) private String typeCode; //屬性id(需要在數(shù)據(jù)字典定義) private String propertityId; //信息可視范圍,部門可見(jiàn)或者整個(gè)單位可見(jiàn),值是組織結(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; //開(kāi)始時(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 '信息可視范圍,部門可見(jiàn)或者整個(gè)單位可見(jiàn),值是組織結(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ā)布表';
頁(yè)面
<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/> 有效期開(kāi)始時(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")//頁(yè)面寫入數(shù)據(jù)庫(kù)時(shí)格式化 @JSONField(format="yyyy-MM-dd")//數(shù)據(jù)庫(kù)導(dǎo)出頁(yè)面時(shí)json格式化
即
修改contoller
既然java.lang.String和java.util.Date之間可以自動(dòng)轉(zhuǎn)換了,后臺(tái)就不需要通過(guò)request獲取參數(shù)來(lái)進(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.dbjr.com.cn/article/176268.htm
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的PUT和Delete請(qǐng)求使用
這篇文章主要介紹了SpringBoot中的PUT和Delete請(qǐng)求使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Java并發(fā)應(yīng)用之任務(wù)執(zhí)行分析
這篇文章主要為大家詳細(xì)介紹了JavaJava并發(fā)應(yīng)用編程中任務(wù)執(zhí)行分析的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07微服務(wù)搭建集成Spring Cloud Turbine詳解
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開(kāi)發(fā)便利性巧妙地簡(jiǎn)化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開(kāi)發(fā),最終給開(kāi)發(fā)者留出了一套簡(jiǎn)單易懂、易部署和易維護(hù)的分布式系統(tǒng)開(kāi)發(fā)工具包。下面我們來(lái)詳細(xì)了解一下吧2019-06-06詳解Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue
這篇文章主要為大家詳細(xì)介紹了Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12