报错信息: 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 百度等解决方法
    
     后记
     
     ???美好的一天,还未结束,仍需努力!
     
     码字不易,感谢大家的支持!! ???
    
   
 
