MyBatis 中有多个 return 类型?

Multiple return types in MyBatis?

我有两个 类 A 和 B,每个都有很多字段,还有一个 table 包含所有这些字段的值。我尝试制作 <select><resultMap> 元素,但未成功 return 类型 A 和 B 对象对的列表。

到目前为止我发现的唯一(不太干净)的解决方案是只实现一个 typeHandler 关联到一个列(比如,table 的第一个)并在该代码中创建我需要的两个对象是通过读取传递给类型处理程序的 ResultSet 中的所有列,而不仅仅是作为 String 传递给类型处理程序的列名。

是否有使用结果图的更简洁的解决方案? (注意:我不想对我的 table 进行分区以匹配两种对象类型)。

可以通过使用关联来实现您的目的:查看 Nested Results for Association

的文档
<resultMap id="pairResultMap" type="Pair">
    <association property="a"  resultMap="aResultMap" />
    <association property="b" javaType="B">
        <!-- define mapping here -->
    </association> 
</resultMap>
<resultMap id="aResultMap" type="A">            
    <!-- define mapping here -->
</resultMap>

您可以直接定义 "sub" 结果图 (B) 或引用它 (A)。