日期:2014-05-20  浏览次数:20930 次

mybatis模糊查询出现“索引 1 超出范围”
这是主调用
public class TestMain {
public static void main(String[] args) {

StuDao dao=new StudentDaoImpl();
//6.查找student
for (Student student : dao.queryStudent("iFo")) {
System.out.println(student);
}

}

这是所调用的方法:
public List<Student> queryStudent(String stuName) {
List<Student> list=sessionFactory.openSession().selectList("selectStudent",stuName);
return list;
}

这是xml配置

<select id="selectStudent" parameterType="String" resultType="entity.Student">
select * from student where stuName like '%#{stuName}%'
</select>

查询的时候会报


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException
### Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
### The error may involve entity.selectStudent-Inline
### The error occurred while setting parameters
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:73)
at daoImpl.StudentDaoImpl.queryStudent(StudentDaoImpl.java:58)
at test.TestMain.main(TestMain.java:39)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:698)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:707)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1015)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:53)
at $Proxy1.setString(Unknown Source)
at org.apache.ibatis.type.StringTypeHandler.setNonNullParameter(StringTypeHandler.java:12)
at org.apache.ibatis.type.StringTypeHandler.setNonNullParameter(StringTypeHandler.java:8)
at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:23)
at org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:73)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:61)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:43)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:56)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:40)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:243)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:72)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:79)
... 3 more

增删改实现着也没啥问题,是不是mybatis相对于ibatis有啥新变动的地方,没有注意到?

------解决方案--------------------
select * from student where stuName like '%#{stuName}%' 

这个配置有问题,Mybatis 不能正确换参数。

网上有办法, 
改成 
select * from student where stuName like CONCAT('%','${stuName}','%' ) 
或者
select * from student where stuName like '%'||'${stuName}'||'%'