Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
### The error may exist in StudentMapper.xml
### The error may involve one.StudentMapper.selectByIdDesc
### The error occurred while handling results
### SQL: select id,name,sex from student order by id desc
### Cause: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
在学习 Mybatis 两种取值符号#{}和${}时,写了用 ${}进行查询并降序排列的代码,结果报出了上面的错误

下面是我的表
Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3_第1张图片

下面是我的mapper文件里的代码

<select id="selectByIdDesc"  parameterType="string" resultType="one.Student">
        select id,name,sex from student order by ${value} desc
    </select>

下面是java方法

public static void selectByIdDesc()throws IOException {
    Reader reader = Resources.getResourceAsReader("conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sessionFactory.openSession();
    StudentMapper studentMapper=session.getMapper(StudentMapper.class);

    List<Student> students=studentMapper.selectByIdDesc("id");
    System.out.println(students);
    session.close();
}

接口

List<Student> selectByIdDesc(String id);

找了好久愣是找不到错误在哪,后来随意改了一下sql语句里的代码

把select后面的各项换成了*,就成功了
<select id="selectByIdDesc"  parameterType="string" resultType="one.Student">
    select * from student order by ${value} desc
</select>

查询结果
在这里插入图片描述

原来是我写的要查询的项和student表里一共有的列个数不一致。。。。。
这样的错误估计也不会有别人犯了,呜呜呜。

你可能感兴趣的:(Mybatis,mybatis,java,mysql,idea)