SpirngBoot+Mybatis进行CRUD时的SQL异常

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值