假设现在有一实体类
package com.test
public class Student{
private String name;
private Integer age;
//省略Get和Set
}
上述实体类对应数据库里student表,那么使用Mybatis去查询所有记录一般都会这样写:
<select id="selectStu" resultType='com.test.Student'>
select * from student
</select>
对应的的Dao里接口名称会这样写:
List<Student> selectStu();
到这里为止都不陌生,可以说相当熟悉。
但是今天,本人在写项目的时候resultType并没有设置具体的实体类而是使用了map,如下:
<select id="selectStu" resultType='map'>
select * from student
</select>
Dao接口写法不变。
结果来说并没有什么问题,依旧是能正常查询的。
但是!但是!但是!
一旦你需要遍历这个List集合,就会报如下的错误:
java.lang.ClassCastException:java.util.HashMap cannot be cast to com.test.Student
嗯.....至于为什么会这样呢,我也不知道~(有机会在研究研究)