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

  • 相关阅读:
    将01字符串转换成数字的办法
    Codeforces Round #180 (Div. 2) AB
    CPU制作过程『转』
    向VECTOR的头部添加元素
    母版页中js操作问题
    操作粘贴板
    XML和关系数据使用XML和数据集类
    XML和关系数据用XML加载数据集
    XPath和XSL转换向XML应用XSL转换
    XML和关系数据从XSD架构创建数据集映射
  • 原文地址:https://www.cnblogs.com/chenyameng/p/13427819.html
Copyright © 2011-2022 走看看