请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

更新用户信息

为什么更新用户信息的时候要新new一个updateUser对象呢?直接用传过来的user不就可以了吗

正在回答 回答被采纳积分+3

插入代码

4回答

徐凌峰 2017-09-24 15:09:40

这里我个人认为<if test="updateTime == null" > update_time = now(), </if> 应该这样吧,因为我们前台不传updateTime过来,这样即使它为null也会调用nwo()函数进行更新啊

0 回复 有任何疑惑可以回复我~
  • Geely #1
    对的,updatetime这里不需要做判断,==null或者!=null这个判断都不需要加,之前在QQ群里说过,这里需要一个勘误
    回复 有任何疑惑可以回复我~ 2017-09-24 15:39:21
提问者 suwei666 2017-07-09 17:31:03

当我添加了

https://img1.sycdn.imooc.com/szimg//5961f77c0001e1f303880038.jpg

,执行updateByPrimaryKeySelective()方法时,mysql才能执行她的now()函数,

https://img1.sycdn.imooc.com/szimg//5961f7c20001ffcf05250036.jpg


0 回复 有任何疑惑可以回复我~
提问者 suwei666 2017-07-09 17:24:57

前辈,当我DEBUG到https://img1.sycdn.imooc.com/szimg//5961f5200001d17606500045.jpg这一行时,我发现新new的updataUser和传过来的user内容是一样的,而且他们的updateTime属性都是为空

https://img1.sycdn.imooc.com/szimg//5961f5b80001fd0502880235.jpg

。执行updateByPrimaryKeySelective()方法时,由于

https://img1.sycdn.imooc.com/szimg//5961f61b0001f6d203450070.jpg

所以更新时间根本不会执行,那这样是不是有点违背初衷?

0 回复 有任何疑惑可以回复我~
Geely 2017-07-09 13:23:14

hi 同学你好


updateByPrimaryKeySelective


hi同学,我们调用的 是这个方法

<update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.User" >
 update mmall_user
 <set >
   <if test="username != null" >
     username = #{username,jdbcType=VARCHAR},
   </if>
   <if test="password != null" >
     password = #{password,jdbcType=VARCHAR},
   </if>
   <if test="email != null" >
     email = #{email,jdbcType=VARCHAR},
   </if>
   <if test="phone != null" >
     phone = #{phone,jdbcType=VARCHAR},
   </if>
   <if test="question != null" >
     question = #{question,jdbcType=VARCHAR},
   </if>
   <if test="answer != null" >
     answer = #{answer,jdbcType=VARCHAR},
   </if>
   <if test="role != null" >
     role = #{role,jdbcType=INTEGER},
   </if>
   <if test="createTime != null" >
     create_time = #{createTime,jdbcType=TIMESTAMP},
   </if>
   <if test="updateTime != null" >
     update_time = now(),
   </if>
 </set>
 where id = #{id,jdbcType=INTEGER}
</update>


注意看他的实现,自己new之后只更新想更新的。不必要的重复更新就不更新了。


0 回复 有任何疑惑可以回复我~
  • 这里我个人认为<if test="updateTime == null" >
         update_time = now(),
       </if>
    应该这样吧,因为我们前台不传updateTime过来,这样即使它为null也会调用nwo()函数进行更新啊
    回复 有任何疑惑可以回复我~ 2017-09-24 15:09:22
  • Geely 回复 徐凌峰 #2
    对的,updatetime这里不需要做判断,==null或者!=null这个判断都不需要加,之前在QQ群里说过,这里需要一个勘误
    回复 有任何疑惑可以回复我~ 2017-09-24 15:39:05
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号