SSM框架的动态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 != ''">
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>

img


我也是按照书上敲的,为什么错了呀

请注意看控制台的输出这两句话,

img


第一处打印了执行的sql语句,第二处提示了语法错误的位置,update user where id = ?,是不是你在测试的时候所有的set都没有进入呢,构建了一个错误的sql语句。

【以下回答由 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>