Mybatis之ResultMap的使用詳解


Mybatis的定義

Mybatis >MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可 >以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。

Mybaits如何使用,這里有一篇關於Mybatis的介紹,沒有使用過的童鞋可以參考,今天主要說的是ResultMap。

ReultMap

ResultMap是Mybatis最強大的元素,它可以將查詢到的復雜數據(比如查詢到幾個表中數據)映射到一個結果集當中。

ResultMap包含的元素:

<!--column不做限制,可以為任意表的字段,而property須為type 定義的pojo屬性-->
<resultMap id="唯一的標識" type="映射的pojo對象">
<id column="表的主鍵字段,或者可以為查詢語句中的別名字段" jdbcType="字段類型" property="映射pojo對象的主鍵屬性" />
<result column="表的一個字段(可以為任意表的一個字段)" jdbcType="字段類型" property="映射到pojo對象的一個屬性(須為type定義的pojo對象中的一個屬                        
性)"/>
<association property="pojo的一個對象屬性" javaType="pojo關聯的pojo對象">
<id column="關聯pojo對象對應表的主鍵字段" jdbcType="字段類型" property="關聯pojo對象的主席屬性"/>
<result  column="任意表的字段" jdbcType="字段類型" property="關聯pojo對象的屬性"/>
</association>
<!-- 集合中的property須為oftype定義的pojo對象的屬性-->
<collection property="pojo的集合屬性" ofType="集合中的pojo對象">
<id column="集合中pojo對象對應的表的主鍵字段" jdbcType="字段類型" property="集合中pojo對象的主鍵屬性" />
<result column="可以為任意表的字段" jdbcType="字段類型" property="集合中的pojo對象的屬性" />  
</collection>
</resultMap>

<strong>如果collection標簽是使用嵌套查詢,格式如下:</strong>

<collection column="傳遞給嵌套查詢語句的字段參數" property="pojo對象中集合屬性" ofType="集合屬性中的pojo對象" select="嵌套的查詢語句" > 
</collection>

注意:
collection標簽中的column:要傳遞給select查詢語句的參數,如果傳遞多個參數,格式為column= ” {參數名1=表字段1,參數名2=表字段2} ;

實例場景介紹ResultMap的用法:

簡單需求:一個商品的結果映射;

1、創建商品pojo對象:

public class TShopSku  {
    /**
     * 主鍵ID
     */
    private Long id;

    /**
     * 商品名
     */
private String skuName;

    /**
     * 分類ID
     */
private Long categoryId;


    /**
     * 主鍵ID
     * @return ID 
     */
public Long getId() {
    return id;
}

    /**
     * 主鍵ID,
     * @param id 
     */
public void setId(Long id) {
    this.id = id;
}

    /**
     * 商品名
     * @return SKU_NAME 商品名
     */
public String getSkuName() {
    return skuName;
}

    /**
     * 商品名
     * @param skuName 商品名
     */
public void setSkuName(String skuName) {
    this.skuName = skuName == null ? null : skuName.trim();
}

    /**
     * 分類ID
     * @return CATEGORY_ID 分類ID
     */
public Long getCategoryId() {
    return categoryId;
}

    /**
     * 分類ID
     * @param categoryId 分類ID
     */
public void setCategoryId(Long categoryId) {
    this.categoryId = categoryId;
}

對應的resultMap:

<resultMap id="BaseResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
</resultMap> 

2、商品pojo類添加屬性集合:

  一個商品會有一些屬性,現在需要將查詢出的商品屬性添加到商品對象中,首先需要在原商品pojo類的基礎上中添加屬性的集合:
    /**
     * 屬性集合
     */
private List<TShopAttribute> attributes;

    /**
     * 獲得屬性集合
     */
public List<TShopAttribute> getAttributes() {
    return attributes;
}

    /**
     * 設置屬性集合
     * @param attributes
     */
 public void setAttributes(List<TShopAttribute> attributes) {
    this.attributes = attributes;
 }

將Collection標簽添加到resultMap中,有兩種方式

1、嵌套結果:
對應的resultMap:

<resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
    <collection property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" > 
        <id column="AttributeID" jdbcType="BIGINT" property="id" />
        <result column="attribute_NAME" jdbcType="VARCHAR" property="attributeName" />
    </collection>
</resultMap>

查詢語句:

<select id="getById"  resultMap="basePlusResultMap">
    select s.ID,s.SKU_NAME,s.CATEGORY_ID,a.ID,a.ATTRIBUTE_NAME
    from t_shop_sku s,t_shop_attribute a 
    where s.ID =a.SKU_ID and s.ID = #{id,jdbcType =BIGINT};
</select>

2、關聯的嵌套查詢(在collection中添加select屬性):
商品結果集映射resultMap:

<resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
    <collection column="{skuId=ID}" property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" select="getAttribute" > 
    </collection>
</resultMap>

collection的select會執行下面的查詢屬性語句:

<select id="getAttribute"  resultMap="AttributeResultMap">
    select a.ID,s.ATTRIBUTE_NAME
    from t_shop_attribute a
    where  a.ID = #{skuId,jdbcType =BIGINT};
</select>

屬性結果集映射:

<resultMap id="AttributeResultMap" type="com.meikai.shop.entity.TShopAttribute">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName" />
</resultMap>

BasePlusResultMap包含了屬性查詢語句的Collection,
所以通過下面的查詢商品語句就可獲得商品以及其包含的屬性集合:

<select id="getById"  resultMap="BasePlusResultMap">
    select s.ID,s.SKU_NAME,s.CATEGORY_ID
    from t_shop_sku s
    where  s.ID = #{id,jdbcType =BIGINT};
</select>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM