SpirngBoot+Mybatis进行CRUD时的SQL异常:You have an error in your SQL syntax; check the manual that corresponds to
总结
出现这个异常,一定是因为SQL语句写错了才会出现。
以下是刚才碰到的错误
1、少逗号
<insert id="AddUser" parameterType="com.zj.springboot.bean.User">
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">id,</if>
<if test="username!=null">username,</if>
<if test="age!=null">age,</if>
<if test="city!=null">city,</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="id!=null">#{id}</if>
<if test="username!=null">#{username}</if>
<if test="age!=null">#{age}</if>
<if test="city!=null">#{city}</if>
</trim>
</insert>
在例如#{id}的这个地方就少了",",之后的username之类的,也都少了
2、Value写错位置
出错时的代码是这样的
<insert id="AddUser" parameterType="com.zj.springboot.bean.User">
INSERT INTO user
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="id!=null">id,</if>
<if test="username!=null">username,</if>
<if test="age!=null">age,</if>
<if test="city!=null">city,</if>
</trim>
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">#{id},</if>
<if test="username!=null">#{username},</if>
<if test="age!=null">#{age},</if>
<if test="city!=null">#{city},</if>
</trim>
</insert>
切记:SQL语句最好现在数据库运行了,再拿来改造
3、Mapper.xml文件内有注释
这是让我最抓狂的…居然在里面不能有注释,无论是什么形式的注释都不行,例如这样就不行
<trim prefix="values(" suffix=")" suffixOverrides=",">
<!--<if test="id!=null">#{id},</if>-->
<if test="username!=null">#{username},</if>
中间这样的注释,是绝对不能出现的
以上都为出错的代码,正确的是这样的
<insert id="AddUser" parameterType="com.zj.springboot.bean.User">
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">id,</if>
<if test="username!=null">username,</if>
<if test="age!=null">age,</if>
<if test="city!=null">city,</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="id!=null">#{id},</if>
<if test="username!=null">#{username},</if>
<if test="age!=null">#{age},</if>
<if test="city!=null">#{city},</if>
</trim>
</insert>
分页符,写到这里后又碰见一异常,先贴出来,开始尝试解决
4、写错表名
{
"timestamp": "2019-10-16T09:48:03.802+0000",
"status": 500,
"error": "Internal Server Error",
"message": "\r\n### Error updating database. Cause:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'springboot_test.tbl_emp' doesn't exist\r\n
### The error may exist in file [F:\\IDEA\\springboot_test_05\\target\\classes\\Mapper\\UserMapper.xml]\r\n
### The error may involve defaultParameterMap\r\n
### The error occurred while setting parameters\r\n
### SQL: update tbl_emp set id = ?, username = ?, age = ?, city = ? where id = ?\r\n
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'springboot_test.tbl_emp' doesn't exist\n;
bad SQL grammar [];
nested exception is com.mysql.jdbc.exceptions.jdbc4.
MySQLSyntaxErrorException:
Table 'springboot_test.tbl_emp' doesn't exist",
"path": "/updatauser"
}
好吧,原来是因为我写错了数据库的表名原因,"SQL: update tbl_emp " 这里用成了之前的表,现在的表示user表。
5、查询数据不对
改了表名回来,开始测试修改操作,出现了状态码=0
百度一看,原来是因为数据库中没有相应的id可以修改,所以就出现了受影响行数为0的效果。
再次总结
SQL syntax这个异常还是比较舒服的,毕竟只是SQL语句出错,大多数情况下,把相应的代码直接转换成SQL语句去数据库操作,就能发现异常在哪了。
今天告一段落,晚上接着调试,写无限制参数的CRUD

本文详细解析了在使用SpringBoot与Mybatis进行CRUD操作时遇到的SQL语法异常,包括少逗号、Value位置错误、Mapper.xml文件内有注释、写错表名及查询数据不对等问题,并提供了正确的代码示例。
478

被折叠的 条评论
为什么被折叠?



