解決mybatis的updateByPrimaryKeySelective方法,實體類為null,卻更新了


解決mybatis的updateByPrimaryKeySelective方法,實體類為null,卻更新了

問題

在新的項目中,使用updateByPrimaryKeySelective確實挺方便的,這個方法是更新不是null的字段,並且參數是更新的實體類,所以在更新的時候,直接將主鍵set到實體類,然后需要更新的字段直接set里面就行。但是這次卻不可以了,沒有報錯,就是一直把創建時間newsCreateTime更新成為null,但我更新的時候,確確實實沒有設置創建時間newsCreateTime這個字段。

<update id="updateByPrimaryKeySelective" parameterType="com.cxff.entity.News" >
  update news
  <set >
    <if test="newsType != null" >
      news_type = #{newsType,jdbcType=VARCHAR},
    </if>
    <if test="newsTitle != null" >
      news_title = #{newsTitle,jdbcType=VARCHAR},
    </if>
    <if test="newsCreateTime != null" >
      news_create_time = #{newsCreateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="newsUpdateTime != null" >
      news_update_time = #{newsUpdateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="newsHaveFile != null" >
      news_have_file = #{newsHaveFile,jdbcType=VARCHAR},
    </if>
    <if test="newsShowImg != null" >
      news_show_img = #{newsShowImg,jdbcType=VARCHAR},
    </if>
    <if test="newsAuthor != null" >
      news_author = #{newsAuthor,jdbcType=VARCHAR},
    </if>
    <if test="newsContent != null" >
      news_content = #{newsContent,jdbcType=LONGVARCHAR},
    </if>
  </set>
  where news_id = #{newsId,jdbcType=INTEGER}
</update>
//修改
final News news = new News();
news.setNewsId(id);
news.setNewsTitle(title);
news.setNewsAuthor(author);
news.setNewsContent(content);
news.setNewsType("1");
news.setNewsUpdateTime(new Date());
int suc = newsService.updateNoticeById(news);

解決

因為mysql的緣故,不是mybatis的原因,你在設置數據庫的時候,把create_time類型設置成為了跟隨當前時間更新

image-20200803180311207

所以點了就行,如果是idea的話,需要重新refresh一下你的數據庫,然后rebuild就行了

image-20200803180411382


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM