zoukankan      html  css  js  c++  java
  • 解决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

  • 相关阅读:
    HDU6301 SET集合的应用 贪心
    线段树与树状数组的对比应用
    树状数组
    JDBC链接MySQL数据库
    HDU4686Arc of Dream 矩阵快速幂
    HDU1757矩阵快速幂
    B1013. 数素数 (20)
    B1023. 组个最小数 (20)
    [教材]B1020. 月饼 (25)
    [教材]A1025. PAT Ranking (25)
  • 原文地址:https://www.cnblogs.com/chenyameng/p/13427819.html
Copyright © 2011-2022 走看看