遇到一问题,sql语句老是报错:Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'workNumber = '10521'' at line 13
当时调用的mybatis sql语句是这样的:<update id="doUpdatemessage" parameterType="com.linkdata.domain.Admin" >
update admin
<set >
<if test="threedepartment != null" >
threeDepartment = #{threedepartment,jdbcType=VARCHAR},
</if>
<if test="twodepartment != null" >
twoDepartment = #{twodepartment,jdbcType=VARCHAR},
</if>
<if test="telephone != null" >
telephone = #{telephone,jdbcType=VARCHAR},
</if>
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
</set>
where
adminName = #{adminname,jdbcType=VARCHAR} and
workNumber = #{worknumber,jdbcType=VARCHAR}
</update>
而这句sql语句单单拿到数据库中去修改完全没有问题,在网上查到这种错误可能是因为关键字造成的。他人就有过字段名为“desc”与排序的“DESC”冲突导致这种错误。
而我的错误“workNumber = '10521” 完全跟关键字搭不上边啊。
突然发现错误语句中有这样一句话:The error may involve com.linkdata.mapper.AdminMapper.selectByCondition-Inline
“selectByCondition” 这是我的另一个查询语句。我就赶快查看这个查询语句,结果:
<select id="selectByCondition" resultMap="BaseResultMap"
parameterType="com.linkdata.domain.Admin">
select
<include refid="Base_Column_List" />
from admin
where 1 = 1
<if test="adminname != null">
and adminName = #{adminname,jdbcType=VARCHAR}
</if>
<if test="password != null">
and password = #{password,jdbcType=VARCHAR}
</if>
<if test="worknumber != null" >
workNumber = #{worknumber,jdbcType=VARCHAR}
</if>
</select>
在这个查询中我在末尾的条件“workNumber = #{worknumber,jdbcType=VARCHAR}” 中忘记写“and” 所以一直报的。
也因为我的代码报错的时候调用的是“doUpdatemessage”而使我忽视了对其他语句的检查。
此时我才又想起来,mybatis对sql调用的时候,并不是仅仅检索自己调用的那条sql语句,其他语句的错误同样也会反馈出来。
又是一次教训。