1. 程式人生 > >Mybatis查詢時間欄位顯示為null的處理方案

Mybatis查詢時間欄位顯示為null的處理方案

環境

SpringBoot 1.5 + JDK1.8 + Mybatis3.4

現象

使用Mybatis動態SQL查詢資料後發現,時間欄位顯示為null,這不僅僅是時間格式問題。

解決與總結

跟蹤程式碼之後發現xml檔案中

對映關係

<resultMap id="PcmArticleListDTO" type="com.xxx.xxx.model.PcmArticleListDTO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="CT_ID" jdbcType="VARCHAR" property="ctId" />
    <result column="TITLE" jdbcType="VARCHAR" property="title" />
    <result column="SUBTITLE" jdbcType="VARCHAR" property="subtitle" />
    <result column="CON" jdbcType="VARCHAR" property="con" />
    <result column="CON_H5" jdbcType="VARCHAR" property="conH5" />
    <result column="ABSTRACT" jdbcType="VARCHAR" property="abstracts" />
    <result column="AUTHOR" jdbcType="VARCHAR" property="author" />
    <result column="CT_TIME" jdbcType="TIMESTAMP" property="ctTime" />
    <result column="KW" jdbcType="VARCHAR" property="kw" />
    <result column="IS_AUTODIS" jdbcType="CHAR" property="isAutodis" />
    <result column="DIS_TIME" jdbcType="TIMESTAMP" property="disTime" />
    <result column="AU_STATE" jdbcType="CHAR" property="auState" />
    <result column="PUB_STATE" jdbcType="CHAR" property="pubState" />
    <result column="DEL_TAG" jdbcType="VARCHAR" property="delTag" />
    <result column="CRT_OPT" jdbcType="VARCHAR" property="crtOpt" />
    <result column="CRT_TIME" jdbcType="TIMESTAMP" property="crtTime" />
    <result column="UPD_OPT" jdbcType="VARCHAR" property="updOpt" />
    <result column="UPD_TIME" jdbcType="TIMESTAMP" property="updTime" />
    <result column="PUB_OPT" jdbcType="VARCHAR" property="pubOpt" />
    <result column="PUB_TIME" jdbcType="TIMESTAMP" property="pubTime" />
    <result column="LIKE_NUM" jdbcType="DECIMAL" property="likeNum" />
    <result column="READ_NUM" jdbcType="DECIMAL" property="readNum" />
    <result column="COVER_PATH" jdbcType="VARCHAR" property="coverPath" />
    <result column="CONTENT_TYP" jdbcType="CHAR" property="contentTyp" />
    <result column="URL" jdbcType="VARCHAR" property="url" />
    <result column="IS_TIMEING" jdbcType="CHAR" property="isTimeing" />
    <result column="TIMEING_DATE" jdbcType="TIMESTAMP" property="timeingDate" />
    <result column="REMARK" jdbcType="VARCHAR" property="remark" />
  </resultMap>

動態SQL

<sql id="selectArticleByIdField">${alias}.CT_ID,${alias}.TITLE,${alias}.SUBTITLE,${alias}.CON,${alias}.CON_H5,${alias}.ABSTRACT,${alias}.AUTHOR,${alias}.KW,${alias}.IS_AUTODIS,${alias}.AU_STATE,${alias}.PUB_STATE,${alias}.LIKE_NUM,${alias}.READ_NUM,${alias}.COVER_PATH,${alias}.CONTENT_TYP,${alias}.URL,${alias}.IS_TIMEING,${alias}.REMARK </sql>
  <select id="selectArticleById" resultType="com.bjbde.trade.model.PcmArticleListDTO">
    select
    <include refid="selectArticleByIdField"><property name="alias" value="t1"></property></include>
    from PCM_ARTICLE t1
    where t1.PUB_STATE = '1'
    and t1.DEL_TAG = '0'
    and t1.CT_ID = #{ctId}

SQL中select後面的域中沒有時間相應的欄位,故予以新增,新增後顯示效果

 "ctTime": 1551024000000,

這個是由於時間格式未作處理的結果

解決方案

找到相應的POJO在對應時間屬性上加註解

@JSONField(format = "yyyy-MM-dd HH:mm")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")

整體效果

@JSONField(format = "yyyy-MM-dd HH:mm")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date ctTime;

加上之後,postman重新測試介面,時間格式顯示