今天在实现一个功能时遇到一个问题,解决了很久。结果是#{}与${}使用错误的原因。但是具体原因还不是很清楚,写此篇总结,知道的可以交流。

具体描述为:通过教师的头衔(1高级讲师2首席讲师)及名称进行模糊查询,报如下错误:

org.springframework.dao.TransientDataAccessResourceException:

### Error querying database.  Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

### The error may exist in file [E:\javaEE+android\mooc\workspace\edu-mooc\target\classes\mybatis\mappers\admin\teacher\TeacherMapper.xml]

### The error may involve com.edu.mooc.admin.mapper.TeacherMapper.quearyTeacherListBySolr-Inline

### The error occurred while setting parameters

### SQL: SELECT count(*) FROM EDU_TEACHER WHERE EDU_TEACHER.`STATUS` = 0 AND EDU_TEACHER.IS_STAR = ? AND EDU_TEACHER.`NAME` LIKE '%?%'

### Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

; SQL []; Parameter index out of range (2 > number of parameters, which is 1).; nested exception is java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

参看一些网上解释:就是当设置参数时,没有相应的问号与之匹配(或者根本就没有?号).

分析错误:

Parameter index out of range (2 > number of parameters, which is 1).

翻译为:找到了1个问号,却插入了2个值,导致参数越界(根据得到的信息打印将很容易判断数据是否与数据库字段匹配等小问题)。

看看sql代码:

SELECT

<include refid="edu_teacher_columns"/>

FROM EDU_TEACHER

<where>

EDU_TEACHER.`STATUS` = 0

<if test="isStar > 0">

AND EDU_TEACHER.IS_STAR = #{isStar}

</if>

<if test="keyWords != null and keyWords != '' ">

AND EDU_TEACHER.`NAME` LIKE '%#{keyWords}%'

</if>

ORDER BY SORT DESC,CREATE_TIME ASC

</where>

将'%#{keyWords}%'修改为'%${keyWords}%'后结果正确

日志信息输出结果为:

==>  Preparing: SELECT count(*) FROM EDU_TEACHER WHERE EDU_TEACHER.`STATUS` = 0 AND EDU_TEACHER.IS_STAR = ? AND EDU_TEACHER.`NAME` LIKE '%李%'

==> Parameters: 1(Integer)

<==    Columns: count(*)

<==        Row: 3

<==      Total: 1

==>  Preparing: SELECT EDU_TEACHER.ID, EDU_TEACHER.NAME, EDU_TEACHER.EDUCATION, EDU_TEACHER.CAREER, EDU_TEACHER.IS_STAR, EDU_TEACHER.PIC_PATH, EDU_TEACHER.STATUS, EDU_TEACHER.CREATE_TIME, EDU_TEACHER.UPDATE_TIME, EDU_TEACHER.SUBJECT_ID, EDU_TEACHER.SORT FROM EDU_TEACHER WHERE EDU_TEACHER.`STATUS` = 0 AND EDU_TEACHER.IS_STAR = ? AND EDU_TEACHER.`NAME` LIKE '%李%' ORDER BY SORT DESC,CREATE_TIME ASC limit ?,?

==> Parameters: 1(Integer), 0(Integer), 10(Integer)

<==    Columns: ID, NAME, EDUCATION, CAREER, IS_STAR, PIC_PATH, STATUS, CREATE_TIME, UPDATE_TIME, SUBJECT_ID, SORT

Parameter index out of range (2 > number of parameters, which is 1)的更多相关文章

  1. Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0

    1.错误描述 [ERROR:]2015-05-05 16:35:50,664 [异常拦截] org.hibernate.exception.GenericJDBCException: error ex ...

  2. mybatis中Parameter index out of range (1 > number of parameters, which is 0).

    Parameter index out of range (1 > number of parameters, which is 0).(参数索引超出范围) 在mybatis里面写就是应该是 l ...

  3. java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

    java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). java. ...

  4. java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

    java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at co ...

  5. [转]操作MySQL数据库报出:Parameter index out of range (1 > number of parameters, which is

    原文地址:https://blog.csdn.net/zdx_y/article/details/52072914 对MySQL进行insert操作,控制台抛出以下错误:Parameter index ...

  6. Parameter index out of range (1 > number of parameters, which is 0).

    数据库错误:Parameter   index   out   of   range   (1   >   number   of   parameters,   which   is   0) ...

  7. Parameter index out of range(1 > number of parameters, which is 0)参数索引超出范围

    今天在写项目的过程中,有一个模块是做多选删除操作,通过servlet获得多选框的value组,然后执行sql操作.如下: 1 @RequestMapping( "/delteCouse.do ...

  8. org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback.....Parameter index out of range (1 > number of parameters, which is 0).;

    sql有误,一般是   sql语句少了问号.

  9. jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)

    http://cuisuqiang.iteye.com/blog/1480525   模糊查询like要这样写 注意Object参数和like语法   public static void main( ...

随机推荐

  1. Centos启动Cassandra交互模式失败:No appropriate python interpreter found

    在CentOS6.5安装好Cassandra后,启动交互模式: bin/sqlsh 192.168.10.154 时,报错 No appropriate python interpreter foun ...

  2. ShellExecute调用另外一个进程(demo为一个控制led的一段代码)

     public enum ShowCommands : int         {             SW_HIDE = 0,             SW_SHOWNORMAL = 1,    ...

  3. CentOS_7.2安装Redis_3.0

    一.安装依赖包和开发工具: yum install vim vim-enhanced wget zip unzip telnet ntsysv compat* apr* nasm* gcc gcc* ...

  4. phpize建立php扩展 Cannot find config.m4

    centos  php  安装 memcache 扩展的时候   爆 Cannot find config.m4 错误 解决方案参考以下文章 参考文章 http://blog.csdn.net/wgl ...

  5. PDF 补丁丁 0.5.0.2520 测试版发布:新春快乐!

    新的PDF测试版今天发布了. 新的测试版比旧测试版本增加了如下功能: 合并文件功能可以导出.导入文件列表,方便合并大量文件.一天干不完,保存一下,明天继续来. 合并文件功能可以统一已合并文件的旋转方向 ...

  6. 数据加密标准——DES

    DES算法和DESede算法统称DES系列算法.DES算法是对称加密算法领域中的典型算法,为后续对称加密算法的发展奠定了坚实的基础.但是DES算法密钥偏短,仅有56位,迭代次数偏少,受到诸如查分密码分 ...

  7. 在EC2上搭建L2TP over IPSec VPN服务器

    注意(:wq保存文件 putty登陆用户名为ec2-user) 安装与配置: 环境介绍: OS:CentOS 6.4 x86_64 Minimal 1. 修改 /etc/sysctl.conf,新增如 ...

  8. java连接sql问题

    1.No suitable driver found for Jdbc: 1)确保​jdbc:sqlserver://localhost;user=sa;password=123456;databas ...

  9. 【软件】【diamond】-管脚分配

    未用到的引脚设置,浮空引脚设置.可以设置浮空电压

  10. 洛谷 1004 dp或最大费用流

    思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...