ToB企服应用市场:ToB评测及商务社交产业平台

标题: 解决MySQL报错Error querying database.Cause com.mysql.jdbc.exceptions.j [打印本页]

作者: 南飓风    时间: 2024-9-26 20:58
标题: 解决MySQL报错Error querying database.Cause com.mysql.jdbc.exceptions.j
解决MySQL报错Error querying database. Cause: 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.

1.问题产生

项目测试环节,jdbc毗连过程where子句出现报错问题
  1. org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error querying database.  Cause: 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 'title = 'xxx'' at line 3
  3. ### The error may exist in com/lanyy/dao/BlogMapper.xml
  4. ### The error may involve com.lanyy.dao.BlogMapper.queryBlogIF-Inline
  5. ### The error occurred while setting parameters
  6. ### SQL: select * from blog where 1=1                       title = ?
  7. ### Cause: 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 'title = 'xxxx'' at line 3
复制代码

2.问题排查

   where之后需接条件语句,需写where 1=1,假如引入标签则不要再写,标签会主动添加执行
  

3.问题解决

   引入<where>标签子句
  where元素只会在至少有一个子元素的条件满足情况下才去插入 \ 查询,
  这个“where”标签会知道假如它输入的信息中包含的标签有返回值(参数)的话,它就插入一个‘where’条件。
  此外,假如标签返回的内容是以AND 或 OR 开头的,则它会剔除掉AND或OR。
  1.     <select id="queryBlogIF" parameterType="map" resultType="Blog">
  2.         select * from blog
  3.         <where>
  4.             <if test="title != null">
  5.                 and title = #{title}
  6.             </if>
  7.             <if test="author != null">
  8.             and author = #{author}
  9.             </if>
  10.         </where>
  11.     </select>
复制代码
  不引入标签,只使用sql中where语句,需要在where后参加条件判定
  1. <select id="queryBlogIF" parameterType="map" resultType="Blog">
  2. <!-- 1=1恒等 条件永久成立 直接遍历blog表中所有信息 选择查询title或者author-->
  3.         select * from blog where 1=1
  4.         <if test="title != null">
  5.             and title = #{title}
  6.         </if>
  7.         <if test="author != null">
  8.             and author = #{author}
  9.         </if>
  10. </select>
复制代码
  调试后运行乐成
  

相识更多知识请戳下:
   @Author:懒羊羊

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4