能登陆,登录去测试更新个性签名功能失败
来源:4-10 用户模块剩余接口开发
我还年轻吃苦趁现在
2022-06-29 17:03:09
Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'username' cannot be null
<update id="updateByPrimaryKey" parameterType="com.gd.mall.model.pojo.User">
update imooc_mall_user
set username = #{username,jdbcType=VARCHAR},
`password` = #{password,jdbcType=VARCHAR},
personalized_signature = #{personalizedSignature,jdbcType=VARCHAR},
`role` = #{role,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>int updateByPrimaryKey(User record);
@Override
public void updateInformation(User user) throws MallException {
//更新个性签名
int updateCount = userMapper.updateByPrimaryKey(user);
if (updateCount > 1){
throw new MallException(MallExceptionEnum.UPDATE_FAILED);
}
}void updateInformation(User user) throws MallException;
@PostMapping("/user/update")
@ResponseBody
public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam String signature) throws MallException {
User currentUser = (User) session.getAttribute(Constant.MALL_USER);
if (currentUser == null){
return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
}
User user = new User();
user.setId(currentUser.getId());
user.setPersonalizedSignature(signature);
userService.updateInformation(user);
return ApiRestResponse.success();
}debug user是有值的但是到了 mapper那里好像就没了
1回答
好帮手慕小小
2022-06-29
同学你好,因为传递的user对象中只包含id、signature两个属性,其余属性均为null,故在执行sql时会出现异常。


建议在sql中进行判断,参考代码如下:
update imooc_mall_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
`password` = #{password,jdbcType=VARCHAR},
</if>
<if test="personalizedSignature != null">
personalized_signature = #{personalizedSignature,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 = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
祝学习愉快~
相似问题