實際開發中,POJO 常包含單個關聯對象(一對一)或集合屬性(一對多),需通過多表關聯查詢一次性裝填。MyBatis 提供 <association> 和 <collection> 兩大標簽優雅解決此問題。
<mapper namespace="cn.net.syl.dao.ProductsMapper">
<!-- 基礎結果映射 -->
<resultMap id="BaseResultMap" type="cn.net.syl.model.Products">
<id column="product_id" property="productId" jdbcType="BIGINT" />
<result column="product_name" property="productName" jdbcType="VARCHAR" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="descript" property="descript" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="DOUBLE" />
...
</resultMap>
<!-- 擴展結果映射:包含一對一、一對多 -->
<resultMap id="BigResultMap" type="cn.net.syl.model.Products" extends="BaseResultMap">
<!-- 一對一:產品擴展信息 -->
<association property="prodExt" javaType="cn.net.syl.model.ProductsExt">
<id column="product_ext_id" property="productExtId" jdbcType="BIGINT" />
<result column="product_id" property="productId" jdbcType="BIGINT" />
...
</association>
<!-- 一對多:產品預覽圖片列表 -->
<collection property="prodPropImgList" ofType="cn.net.syl.model.ProductsPreviewImages">
<id column="prev_id" property="prevId" jdbcType="BIGINT" />
<result column="product_id" property="productId" jdbcType="BIGINT" />
...
</collection>
</resultMap>
<!-- 多表聯查語句 -->
<select id="getProductInfo" resultMap="BigResultMap" parameterType="java.lang.Long">
SELECT
a.*, b.detail_descript,
c.image_url, c.product_id, c.property_id, c.value_id,
d.value_name
FROM syl_products a
JOIN syl_products_ext b ON a.product_id = b.product_id
JOIN syl_products_preview_images c ON a.product_id = c.product_id
JOIN syl_property_values d ON c.value_id = d.value_id
WHERE a.product_id = #{productId}
</select>
</mapper>
public class Products {
private Long productId;
private String productName;
// ... 其他基礎字段
// 一對一關聯對象
private ProductsExt prodExt;
// 一對多集合屬性
private List<ProductsPreviewImages> prodPropImgList;
// 另一個一對多示例
private List<ProductPropertyEntity> baseProdPropList;
// Getter/Setter 省略...
}
| 標簽 | 適用關係 | 核心屬性 | 去重依據 |
|---|---|---|---|
<association> | 一對一 / 多對一 | property、javaType | 結果映射中的 <id> 字段 |
<collection> | 一對多 | property、ofType | 集合元素映射中的 <id> 字段 |
BigResultMap extends="BaseResultMap" 避免基礎字段重複定義;<id> 標簽指定的列必須能唯一標識對象,否則會產生重複對象;column 需一致,必要時用 AS 起別名;select 屬性配置嵌套懶加載,但通常聯查性能更優。resultMap + 聯查 SQL,避免 N+1 問題;<result> 配置;ResultHandler 流式處理大結果集;