一、问题描述
在mybatis的xml配置文件中配置select后,查询报错:
Result type not match…或
Result Maps collection does not contain value for java.lang.String或
Could not find result map java.lang.String等异常。
二、问题原因
1、在dao.xml文件中返回值定义为 resultType=“java.util.List” ,则dao层会报错,提示Result type not match…;
2、.xml文件中的resultMap设置成了基础数据类型(String,Integer,Double等),例如: resultMap="java.lang.String"
三、解决方法
将resultMap="java.lang.String"修改成resultType="java.lang.String" 即可。
注意:resultType用于sql映射文件中定义返回值类型,返回值有基本类型、对象类型、List类型、Map类型等。返回List集合时,需要将resultType的值定义为集合中元素类型,而不是返回集合本身。
它们之间的区别可查看以下文章:
Mybatis中的resultType和resultMap的区别