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

  • 相关阅读:
    第1章:路线图
    第0章:战胜恐惧和懊悔
    c++ 变量 定义 声明
    两个无符号数相减 可以得到负数
    c++ cast operator(类型转换操作符)
    C++中static 的使用方式,以及与c中的static的区别
    重载overload 重写override 重定义redefining
    用到位运算的题目
    C++ 虚函数表解析
    虚函数 动态绑定 实现方式是:虚函数表
  • 原文地址:https://www.cnblogs.com/chenyameng/p/13427819.html
Copyright © 2011-2022 走看看