亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Mybatis中resultMap的使用總結(jié)

瀏覽:2日期:2023-10-18 18:42:44

Mybatis的介紹以及使用:http://www.mybatis.org/mybatis-3/zh/index.html

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

resultMap包含的元素:

<!--column不做限制,可以為任意表的字段,而property須為type 定義的pojo屬性--><resultMap 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對象對應表的主鍵字段' 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對象對應的表的主鍵字段' jdbcType='字段類型' property='集合中pojo對象的主鍵屬性' /> <result column='可以為任意表的字段' jdbcType='字段類型' property='集合中的pojo對象的屬性' /> </collection></resultMap>

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

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

注意:<collection>標簽中的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; }

對應的resultMap:

<resultMap 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類的基礎上中添加屬性的集合:

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

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

1、嵌套結(jié)果:

對應的resultMap:

<resultMap 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 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 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 resultMap='AttributeResultMap'> select a.ID,s.ATTRIBUTE_NAME from t_shop_attribute a where a.ID = #{skuId,jdbcType =BIGINT};</select>

屬性結(jié)果集映射:

<resultMap 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 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

相關(guān)文章:
主站蜘蛛池模板: 欧美国产激情二区三区 | 人成免费 | 欧美亚洲偷图色综合91 | 国产精品毛片天天看片 | 99热这里有免费国产精品 | 国产综合91 | 玖玖国产在线观看 | julia一区二区三区中文字幕 | 91精品福利久久久 | 免费看欧美日韩一区二区三区 | 国产毛片久久国产 | 公么吃奶满足了我苏媚 | 亚洲天堂毛片 | 国产日韩欧美亚洲青青草原 | 久久精品视频免费 | 精品自在线 | 欧美成人丝袜一区二区 | 成人男女网18免费软件大全 | 风间由美中文字幕亚洲一区 | 三黄色 | 国语自产自拍秒拍在线视频 | 欧美亚洲国产另类在线观看 | 成人国产精品免费网站 | 欧美午夜理伦三级在线观看 | 亚洲综合网在线观看 | 国产肥老妇视频∵ | 午夜国产在线视频 | 精品国产一区二区三区久久影院 | 国产精品小黄鸭一区二区三区 | 国产精品久久久久久网站 | 国产亚洲一欧美一区二区三区 | 成人国产综合 | 国产精品成人免费 | 好爽好黄的视频 | 国产精品美女久久久久久 | 伊人蕉久中文字幕无码专区 | 真人女人一级毛片免费视频观看 | 久久国产精品久久久久久小说 | 久久黄网 | 一本之道无吗一二三区 | 在线看成人 |