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

具体描述为:通过教师的头衔(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. 关于offsetWidth,offsetHeight,offsetTop,offsetLeft和二维数组的声明

    offsetWidth,offsetHeight,offsetTop,offsetLeft 为只读状态,返回的值是int形式 只读形式即不能通过修改其值的大小. 想要修改某元素的这些值的大小(widt ...

  2. 推荐两个谷歌的json-view插件(附带下载分享地址)

    1.JSONView 网盘下载地址:http://pan.baidu.com/s/1hrGlaVa 效果图: 2.JSON-handle 网盘下载地址:http://pan.baidu.com/s/1 ...

  3. C# DateTime 格式化 奇怪问题!

    使用 DateTime.Now.ToString("MM/dd") 本地显示 06/16 window sevcie 2003  显示是 06-16 解决方法: DateTime. ...

  4. python类中super()和__init__()的区别

    class Base(object):     def __init__(self): print 'Base create' class childB(Base): def __init__(sel ...

  5. 断言(assert)的用法

    我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非“报错”. 在经过对其进行一定了解之后,对其作用及用法有了一定的了解,assert()的用法像是一种“契约式编程”,在我的理解 ...

  6. [原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。

    首先介绍基本WindowsApi: public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 函 ...

  7. ASCII表

    ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧 ...

  8. Android ORM应用开发框架KJFrameForAndroid使用详解

    本文将为大家介绍一款Android ORM应用开发框架KJFrameForAndroid,很多时候我们也叫它KJLibrary. KJFrameForAndroid简介 KJFrameForAndro ...

  9. 显示图片的(自定义)吐司Toast

    一般我们提示的时候都是直接提示文字的,其实Toast也可以显示图片 常用方法 Toast.makeText(context,text,duration)这返回一个Toast对象 toast.setDu ...

  10. Openwrt 编译报错:rootfs image is too big解决方法

    修改: tools/firmware-utils/src/mktplinkfw2.c static struct flash_layout layouts[] = { { .id = "8M ...