Parameter index out of range (2 > number of parameters, which is 1)
今天在实现一个功能时遇到一个问题,解决了很久。结果是#{}与${}使用错误的原因。但是具体原因还不是很清楚,写此篇总结,知道的可以交流。
具体描述为:通过教师的头衔(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)的更多相关文章
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- [转]操作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 ...
- Parameter index out of range (1 > number of parameters, which is 0).
数据库错误:Parameter index out of range (1 > number of parameters, which is 0) ...
- Parameter index out of range(1 > number of parameters, which is 0)参数索引超出范围
今天在写项目的过程中,有一个模块是做多选删除操作,通过servlet获得多选框的value组,然后执行sql操作.如下: 1 @RequestMapping( "/delteCouse.do ...
- org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback.....Parameter index out of range (1 > number of parameters, which is 0).;
sql有误,一般是 sql语句少了问号.
- jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)
http://cuisuqiang.iteye.com/blog/1480525 模糊查询like要这样写 注意Object参数和like语法 public static void main( ...
随机推荐
- flask+html selected 根据后台数据设定默认值
先给代码 <script> $("#selector_political_status").val('{{ archive.political_status }}'); ...
- CIDR风格
CIDR地址中包含标准的32位IP地址和有关网络前缀位数的信息. eg: 地址:192.168.10.0/24,其中/24标示前面地址中的前24位代表网络部分,其余代表主机部分. 11000000 1 ...
- CSS样式应用
CSS样式应用的方法: (1)行内样式,将css样式直接放到标签当中,一般都是放入标签的style属性中,它是最方便的一种样式,也是最不方便修改的样式.如下: (2)内嵌式,通过将css写在网页源文件 ...
- 纯JS 将table表格导出到excel
html <div > <button type="button" onclick="getXlsFromTbl('tableExcel','myDiv ...
- 使用dreamweaver去掉文本中的空格和换行
当我们从其他地方拷贝文本到网页,在html代码中会自动带有空格和换行,手动去掉很麻烦,今天试着用dreamweaver去了一下,方法如下: 1.点击Ctrl+F,打开“查找和替换”窗口 2‘见下图:
- powerdesigner,eclipse整合安装
com.sybase.powerdesigner.eclipse.link path=D:\\dbs\\dbtools\\SAP\\PowerDesigner16
- NetworkComms V3 模拟登陆
演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...
- JVM 平台上的各种语言的开发指南
JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...
- hadoop 2.6配置记录
本地hadoop配置 1)core-site.xml <?xml version="1.0" encoding="UTF-8"?> <?xml ...
- PHP造PDO对象和事务功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...