resultType
的使用详解resultType
的含义resultType
指的是每一行查询结果的 Java 类型,而不是整个结果集的类型。resultType="java.lang.String"
:表示每一行是一个字符串。resultType="com.example.User"
:表示每一行是一个 User
对象。resultType
与方法返回值类型的关系List
中。List<对象>
,但 resultType
只指定单行结果类型。
ListgetFilePathsById(@Param("attachmentIds") List attachmentIds);
java.lang.String
而不是 List
?MyBatis 处理机制
List
中。resultType
就是用于指定单行结果的类型,而不是整个列表的类型。SQL 查询:
select file_path from vb_knowledge_base_attachment where id in (1, 2, 3);
file_path ----------- /path/to/file1 /path/to/file2 /path/to/file3
String
类型,因此 resultType="java.lang.String"
。MyBatis 自动封装
String
封装到一个 List
中。List
。Result type not match for select... targetType: java.lang.String
resultType
设置错误。resultType
设置为单行数据类型,例如 java.lang.String
。List
。Mapper 接口 方法:
List
List
。MyBatis 映射文件:
resultType="list"
是错误的,MyBatis 没有这个类型。parameterType="java.util.List"
是不必要的,可以简化。resultType="java.lang.String"
List
。parameterType="java.util.List"
List
,因此指定类型为 java.util.List
。collection="attachmentIds"
collection
指定为方法参数的名称,即 @Param("attachmentIds")
。@Param
注解
list
,更加规范。resultType
表示每一行数据类型,而非整个结果集的类型。List
,MyBatis 自动将每行结果封装为列表。resultType
设置为集合类型。