报错信息: java.sql.SQLSyntaxErrorException: near ‘) tmp_count‘

3 篇文章 0 订阅
2 篇文章 0 订阅

报错信息: java.sql.SQLSyntaxErrorException: near ‘) tmp_count’

报错信息: java.sql.SQLSyntaxErrorException: near ‘) tmp_count’

错误原因 : 语句错误,因为当时项目里粗心导致多写了一个 > 符号,导致该原因;

解决 : 语法检查, 删除这个多余的 > 符号,即可;

下面是报错具体信息展示 :

图一: 后台报错信息

### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: 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 ') tmp_count' at line 5
    
### The error may exist in file [D:\22\JAVA\code\crm_test\target\classes\mappers\RoleMapper.xml]

### The error may involve com.itkaka.crm.dao.RoleMapper.selectByParams-Inline

### The error occurred while setting parameters

### SQL: select count(0) from (select       id, role_name, role_remark, create_date, update_date, is_valid         from t_role      WHERE is_valid = 1 >) tmp_count

### Cause: java.sql.SQLSyntaxErrorException: 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 ') tmp_count' at line 5    
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 ') tmp_count' at line 5] with root cause

java.sql.SQLSyntaxErrorException: 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 ') tmp_count' at line 5

在这里插入图片描述

图二 : 前台错误图片如下:
在这里插入图片描述

图三 : 删除多余的 > 之后 ,错误和正确的代码比对如下:

<!-- 分页多条件列表查询 -->
  <select id="selectByParams" parameterType="com.itkaka.crm.query.RoleQuery" resultType="com.itkaka.crm.vo.Role">
    select   <include refid="Base_Column_List"></include>
    from     t_role
    <where>
             is_valid = 1
          <if test="null != roleName and '' != roleName">
             and role_name like concat('%',#{roleName},'%')
          </if>
    </where>>
  </select>
<!-- 错误在第10行的 where 后面多一个大于号 -->

正确的SQL查询代码如下:

<!-- 正确的 分页多条件列表查询 -->
  <select id="selectByParams" parameterType="com.itkaka.crm.query.RoleQuery" resultType="com.itkaka.crm.vo.Role">
    select 	<include refid="Base_Column_List"></include>
    from 	t_role
    <where>
      		is_valid = 1
      <if test="null != roleName and '' != roleName">
        	and role_name like concat('%',#{roleName},'%')
      </if>
    </where>
  </select>

前台测试效果图:
在这里插入图片描述

本次报错的其余可能原因:

情况一: 出现空指针
<!-- 正确的 分页多条件列表查询 -->
  <select id="selectByParams" parameterType="com.itkaka.crm.query.RoleQuery" resultType="com.itkaka.crm.vo.Role">
    select <include refid="Base_Column_List"></include>
    from t_role
    <where>
      is_valid = 1
      <if test="null != roleName and '' != roleName">
        and role_name like concat('%',#{roleName},'%')
      </if>
    </where>
  </select>

<!-- 错误1 空指针 -->
  <select id="selectByParams" parameterType="com.itkaka.crm.query.RoleQuery" resultType="com.itkaka.crm.vo.Role">
    select <include refid="Base_Column_List"></include>
    from t_role
    where
      
      <if test="null != roleName and '' != roleName">
        and role_name like concat('%',#{roleName},'%')
      </if>
    
  </select>
<!-- 假如 roleName 为空 where 后面啥也没有也会报错-->

解决方式一: 修改成为 < where > … < /where >

解决方式二: 在 where 后面加一个恒等式 比如 1=1

方式二 举例如下:

<select id="selectByParams" parameterType="com.itkaka.crm.query.RoleQuery" resultType="com.itkaka.crm.vo.Role">
    select <include refid="Base_Column_List"></include>
    from t_role
    where
       1= 1
      <if test="null != roleName and '' != roleName">
        and role_name like concat('%',#{roleName},'%')
      </if>   
  </select>
情况二 : 空格建的使用, 有可能是缺少空格键
情况三 : 语句最后多一个分号
情况四 : 出现这个问题说明字段为null,然后传过来的值不会为null,直接会让你整段语句变为空,那么就报错了,所以在mybatis里要加if判断

拓展 : 出现tem_count 这样的 是因为分页插件PageHelper 的方法,会有count()总条数
如果查询量过大就会出现这种情况,会将数据库卡死

可以尝试下 主sql 把数据拉回来

ResultSet rs = ps.executeQuery();
rs.last();
int count = rs.getRow();

错误总结: SQL 语句严格注意语法; 日常写代码等时候一定要仔细再仔细;

把数据拉回来

ResultSet rs = ps.executeQuery();
rs.last();
int count = rs.getRow();

错误总结:

  • SQL 语句严格注意语法; 日常写代码等时候一定要仔细再仔细;
  • 同时大家一定学会看错误日志,找到 错误定位 , debug 百度等解决方法

后记
👉💕💕美好的一天,还未结束,仍需努力!
码字不易,感谢大家的支持!! 🌹🌹🌹

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值