經過來回的檢查,發現是SQL語句沒有加條件語句
修改前
<update id="update">
UPDATE smbms_user SET userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole},createdBy=#{createdBy},creationDate=#{creationDate},idPicPath=#{idPicPath}
</update>
修改後
<update id="update">
UPDATE smbms_user SET userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole},createdBy=#{createdBy},creationDate=#{creationDate},idPicPath=#{idPicPath}
WHERE id=#{id}
</update>
測試類
//更新
User user=new User();
user.setId(21);
user.setUserCode("21");
user.setUserName("lalaalall");
int count=session.getMapper(userDao.class).update(user);
重點在於WHERE id=#{id}
沒有加,那麼表中的數據,都會變成你想加的那一條
但是這個更新,會把沒有其餘沒有更新的其他值變爲空
可修改SQL語句
<update id="update">
UPDATE smbms_user
<set>
<if test="userCode!=null and userCode!=''">userCode=#{userCode},</if>
<if test="userName!=null and userName!=''">userName=#{userName},</if>
<if test="userPassword!=null and userPassword!=''">userPassword=#{userPassword},</if>
<if test="gender!=null">gender=#{gender},</if>
<if test="birthday!=null">birthday=#{birthday},</if>
<if test="phone!=null and phone!=''">phone=#{phone},</if>
<if test="address!=null and address!=''">address=#{address},</if>
<if test="userRole!=null">userRole=#{userRole},</if>
<if test="modifyBy!=null">modifyBy=#{modifyBy},</if>
<if test="true">modifyDate=NOW(),</if>
<if test="idPicPath!=null and idPicPath!=''">idPicPath=#{idPicPath},</if>
</set>
WHERE id=#{id}
</update>
User user = new User();
user.setUserName("小楊過");
user.setId(14);
int count = session.getMapper(UserDao.class).update(user);