mybatis模糊查询出现“索引 1 超出范围”

lazyfox 2012-01-16 12:36:35
这是主调用
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有啥新变动的地方,没有注意到?
...全文
1025 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylovep 2012-01-16
  • 打赏
  • 举报
回复
楼主的sql语句返回的结果集不止一个??
lazyfox 2012-01-16
  • 打赏
  • 举报
回复
我在网上找到解决办法了。就是在select语句中。
stuName不能跟JavaBean中的参数名一样。
成功地是这样的||
"select * from student where stuName like '%${value}%'"
这个我试了下,value不能写成其它的。只要写成其它的就报错。
原文地址是在看到的!
http://hi.baidu.com/sonmeika/blog/item/c6805ecbf90fd612be09e6c5.html
出现这个原因是因为sql出错。mybatis中#这个在sql语句中好像是个关键字。要改成$,然后我又出错一个错误。“There is no getter for property named 'index' in 'class java.lang.Integer'”
然后看到上面的地址解决了。

作者是这样说的:“当传入普通变量,而不是Student的成员时,使用value代替该变量。例如我在StudentMapper中定义一个返回List的成员函数,List<Student> selectStudents(int index);,在Student.xml中使用变量名时不能写“index”,而应该使用“value”来代替这个Integer。”
不过我好奇的是按照他这么说,那我根据id查询的时候 也应该这样写。可是我没有这样写也没报错。。。


好乱。。。。反正有此类问题的朋友慢慢看吧。语文学的太差了。。。
lazyfox 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ylovep 的回复:]

楼主的sql语句返回的结果集不止一个??
[/Quote]
你是说这个resultType返回值类型吗?这个好像是设置实体类。然后那边接收用list就好了,在之前的查询所有信息有过。
lazyfox 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lovexp2010 的回复:]

select * from student where stuName like '%#{stuName}%'

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

网上有办法,
改成
select * from student where stuName like CONCAT('%','${stuName}','%' )
或者
select * from stude……
[/Quote]

这两种方法也都不可以。
我尝试过写成在xml文件中写成select * from student where stuName like '#{stuName}'
然后在调用这个方法,在传递参数的时候写成%iFo%
就是这样
for (Student student : dao.queryStudent("%iFo%")) {
System.out.println(student);
}
也不行。
Jeff-HT-Lee 2012-01-16
  • 打赏
  • 举报
回复
select * from student where stuName like '%#{stuName}%'

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

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

我不知道 CONCAT 和 || 是不是SQL标准

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧