改错误原因为sql语句拼写错误
xml的update语句为
<update id="updateStudent" parameterType="java.util.Map">
update tab_stu
<if test="stu_name != null">
stu_name = #{stu_name},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="classInfo != null">
c_id = #{classInfo},
</if>
where stu_id = #{stu_id}
</update>
可以看出每句话后面都会有一个,逗号 生成的sql语句则会在where前多一个,即,update tab_stu stu_name = ?, sex = ?, c_id = ?, where stu_id = ?
此时要使用<trim></trim>标签进行去字符串句末的都好功能即代码改为
<update id="updateStudent" parameterType="java.util.Map">
update tab_stu
<trim prefix="set" suffixOverrides=","> <!--此处未来去掉生成在where前的逗号-->
<if test="stu_name != null">
stu_name = #{stu_name},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="classInfo != null">
c_id = #{classInfo},
</if>
</trim>
where stu_id = #{stu_id}
</update>
结果生成的sql语句为 update tab_stu set stu_name = ?, sex = ?, c_id = ? where stu_id = ?
其实<trim></trim>还有可以对字符串头的多余字符进行操作,此处不做演示
mybatis学习中update报错Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: (三)