Mybatis中resultMap的使用總結(jié)
Mybatis的介紹以及使用:http://www.mybatis.org/mybatis-3/zh/index.html
resultMap是Mybatis最強大的元素,它可以將查詢到的復(fù)雜數(shù)據(jù)(比如查詢到幾個表中數(shù)據(jù))映射到一個結(jié)果集當(dāng)中。
resultMap包含的元素:
<!--column不做限制,可以為任意表的字段,而property須為type 定義的pojo屬性-->
<resultMap id="唯一的標(biāo)識" type="映射的pojo對象">
<id column="表的主鍵字段,或者可以為查詢語句中的別名字段" jdbcType="字段類型" property="映射pojo對象的主鍵屬性" />
<result column="表的一個字段(可以為任意表的一個字段)" jdbcType="字段類型" property="映射到pojo對象的一個屬性(須為type定義的pojo對象中的一個屬性)"/>
<association property="pojo的一個對象屬性" javaType="pojo關(guān)聯(lián)的pojo對象">
<id column="關(guān)聯(lián)pojo對象對應(yīng)表的主鍵字段" jdbcType="字段類型" property="關(guān)聯(lián)pojo對象的主席屬性"/>
<result column="任意表的字段" jdbcType="字段類型" property="關(guān)聯(lián)pojo對象的屬性"/>
</association>
<!-- 集合中的property須為oftype定義的pojo對象的屬性-->
<collection property="pojo的集合屬性" ofType="集合中的pojo對象">
<id column="集合中pojo對象對應(yīng)的表的主鍵字段" jdbcType="字段類型" property="集合中pojo對象的主鍵屬性" />
<result column="可以為任意表的字段" jdbcType="字段類型" property="集合中的pojo對象的屬性" />
</collection>
</resultMap>
如果collection標(biāo)簽是使用嵌套查詢,格式如下:
<collection column="傳遞給嵌套查詢語句的字段參數(shù)" property="pojo對象中集合屬性" ofType="集合屬性中的pojo對象" select="嵌套的查詢語句" > </collection>
注意:<collection>標(biāo)簽中的column:要傳遞給select查詢語句的參數(shù),如果傳遞多個參數(shù),格式為column= ” {參數(shù)名1=表字段1,參數(shù)名2=表字段2} ;
以下以實例介紹resultMap的用法:
一、簡單需求:一個商品的結(jié)果映射;
1、創(chuàng)建商品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;
}
對應(yīng)的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>
二、商品pojo類添加屬性集合:
一個商品會有一些屬性,現(xiàn)在需要將查詢出的商品屬性添加到商品對象中,首先需要在原商品pojo類的基礎(chǔ)上中添加屬性的集合:
/**
* 屬性集合
*/
private List<TShopAttribute> attributes;
/**
* 獲得屬性集合
*/
public List<TShopAttribute> getAttributes() {
return attributes;
}
/**
* 設(shè)置屬性集合
* @param attributes
*/
public void setAttributes(List<TShopAttribute> attributes) {
this.attributes = attributes;
}
將Collection標(biāo)簽添加到resultMap中,這里有兩種方式:
1、嵌套結(jié)果:
對應(yīng)的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、關(guān)聯(lián)的嵌套查詢(在collection中添加select屬性):
商品結(jié)果集映射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會執(zhí)行下面的查詢屬性語句:
<select id="getAttribute" resultMap="AttributeResultMap">
select a.ID,s.ATTRIBUTE_NAME
from t_shop_attribute a
where a.ID = #{skuId,jdbcType =BIGINT};
</select>
屬性結(jié)果集映射:
<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>
到此這篇關(guān)于Mybatis中resultMap的使用總結(jié)的文章就介紹到這了,更多相關(guān)Mybatis resultMap 使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot事件機(jī)制相關(guān)知識點匯總
這篇文章主要介紹了SpringBoot事件機(jī)制相關(guān)知識點匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
IDEA-SpringBoot項目Debug啟動不了(卡住不動)的原因分析
這篇文章主要介紹了IDEA-SpringBoot項目Debug啟動不了(卡住不動)的原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring Cloud下實現(xiàn)用戶鑒權(quán)的方案
Java下常用的安全框架主要有Spring Security和shiro,都可提供非常強大的功能,但學(xué)習(xí)成本較高。但在微服務(wù)下鑒權(quán)又會對服務(wù)有一定的入侵性。 因此,本文將介紹Spring Cloud下實現(xiàn)用戶鑒權(quán)的方案,感興趣的同學(xué)可以關(guān)注一下2021-11-11
java?for循環(huán)內(nèi)執(zhí)行多線程問題
這篇文章主要介紹了java?for循環(huán)內(nèi)執(zhí)行多線程問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決
這篇文章主要介紹了SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Java在指定路徑上創(chuàng)建文件提示不存在解決方法
在本篇文章里小編給大家整理的是一篇關(guān)于Java在指定路徑上創(chuàng)建文件提示不存在解決方法,有需要的朋友們可以參考下。2020-02-02
Java中StringBuffer和StringBuilder_動力節(jié)點Java學(xué)院整理
StringBuffer、StringBuilder和String一樣,也用來代表字符串。String類是不可變類,StringBuffer則是可變類,任何對它所指代的字符串的改變都不會產(chǎn)生新的對象。本文重點給大家介紹String、StringBuffer、StringBuilder區(qū)別,感興趣的朋友一起看看吧2017-04-04
Springboot整合GateWay+Nacos實現(xiàn)動態(tài)路由
本文主要介紹了Springboot整合GateWay+Nacos實現(xiàn)動態(tài)路由,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08

