<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.experience.dao.User_update_dao">
<update id="modifyUser" parameterType="com.experience.entity.User">
update user
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
<if test="tel != null and tel != ''">
tel = #{tel},
</if>
<if test="address != null and address != ''">
address = #{address}
</if>
</set>
where id = #{id}
</update>
</mapper>
请注意看控制台的输出这两句话,
【以下回答由 GPT 生成】
问题分析: 根据提供的代码和问题描述,可能的问题是在XML映射文件中的动态SQL语句中,Set元素中的最后一个字段后面有一个逗号。这可能导致在编译过程中出现错误。
解决方案: 将逗号从最后一个字段的后面移动到字段的前面。
修改后的代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.experience.dao.User_update_dao">
<update id="modifyUser" parameterType="com.experience.entity.User">
update user
<set>
<if test="username != null and username != ''">
username = #{username}
</if>
<if test="password != null and password != ''">
<if test="username != null and username != ''">,</if>
password = #{password}
</if>
<if test="sex != null and sex != ''">
<if test="username != null and username != ''">,</if>
sex = #{sex}
</if>
<if test="tel != null and tel != ''">
<if test="username != null and username != ''">,</if>
tel = #{tel}
</if>
<if test="address != null and address != ''">
<if test="username != null and username != ''">,</if>
address = #{address}
</if>
</set>
where id = #{id}
</update>
</mapper>
注意:上述代码是根据你提供的问题进行的修改,但是如果业务逻辑允许,你也可以考虑使用MyBatis的动态SQL标签来简化代码。
【相关推荐】
出现异常的原因是:你调用modifyUser方法时,没有传递参数或参数为空。因为你参数都是空,所以mybatis构建的sql语句为:update user where id = ?,这是个错误的语句。
如果避免此问题:1. 调用modifyUser方法时,校验参数,使之必须存在一个id及其他的参数(不能为空)。2. modifyUser方法的SQL语句中,添加一个默认修改字段,如update_time = now(),这样即使用户只传递一个id,sql也不会报错。
望采纳。
参考下面的代码
<update id="updateDept" parameterType="Dept">
update sys_dept
<set>
<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
<if test="orderNum != null">order_num = #{orderNum},</if>
<if test="leader != null">leader = #{leader},</if>
<if test="phone != null">phone = #{phone},</if>
<if test="email != null">email = #{email},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = now()
</set>
where dept_id = #{deptId}
</update>