Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用
Collection
collection: 一個(gè)復(fù)雜的類型關(guān)聯(lián),許多結(jié)果將映射為這種類型property: 這是關(guān)聯(lián)的 JavaBean 中的屬性名, 在 RoleModel 中對(duì)應(yīng) private List<MenuModel> menus;javaType: property 屬性對(duì)應(yīng)的集合類型ofType: property 集合中的泛型,即定義時(shí)泛型所表示的具體類型column: RoleModel 的 id ,作為參數(shù)傳入被調(diào)用的 Select 語(yǔ)句
用法,嵌套在其他的查詢說sql中,寫法 主要以下3種:
第一種:select 語(yǔ)句在其他dao.java對(duì)應(yīng)的 mapper.xml中
<collection property="tProductVideos" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
column="product_id"
select="com.mpi.biz.dao.cust.ProductDao.getProductList">
</collection>第二種:select語(yǔ)句在同一個(gè)mapper.xml中
(與第一種的區(qū)別在于select屬性的值不一樣)
.....
<collection property="tProductVideos" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
column="product_id"
select="getProductList">
</collection>
......
<resultMap id="BaseProduct" type="com.mpi.biz.model.auto.TProduct">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="product_id" property="productId" jdbcType="INTEGER" />
<result column="image_url" property="imageUrl" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="CHAR" />
</resultMap>
<select id="getProductImageList" resultMap="BaseProduct" parameterType="java.lang.Integer">
SELECT i.id, i.product_id, i.image_url,i.type
FROM t_product_image i
WHERE i.product_id = #{productId}
</select>
第三種,如果再一個(gè)查詢中可以直接查詢到所需要的數(shù)據(jù)
但是需要映射到該對(duì)象的屬性上,則可以使用該方式(建議使用上兩種,看起來(lái)更清晰):
<resultMap id="BaseRoleResultMap" type="cn.com.hellowood.springsecurity.model.RoleModel">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="is_active" property="isActive" jdbcType="BOOLEAN"/>
<result column="description" property="description" jdbcType="VARCHAR"/>
<result column="last_update_time" property="lastUpdateTime" jdbcType="TIMESTAMP"/>
<collection property="menus" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
javaType="java.util.ArrayList">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="product_id" property="productId" jdbcType="INTEGER" />
<result column="image_url" property="imageUrl" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="CHAR" />
</collection>
</resultMap>
<select id="getRoles" parameterType="java.lang.Integer" resultMap="BaseRoleResultMap">
SELECT
r.id,
r.name,
r.description,
r.is_active,
r.last_update_time,
m.id,
m.value,
m.display_value,
m.url,
m.category,
m.description,
m.is_active,
m.last_update_time
FROM table1 r
LEFT JOIN t_product p
ON r.id = p.r_id
WHERE r.id = #{roleId,jdbcType=INTEGER}
</select>
association總結(jié)
主要用到的屬性:
property:映射數(shù)據(jù)庫(kù)列的字段或?qū)傩浴?/li>column:數(shù)據(jù)庫(kù)的列名或者列標(biāo)簽別名。與傳遞給resultSet.getString(columnName)的參數(shù)名稱相同。javaType:完整java類名或別名(參考上面的內(nèi)置別名列表)。如果映射到一個(gè)JavaBean,那MyBatis 通常會(huì)自行檢測(cè)到。然而,如果映射到一個(gè)HashMap,那您應(yīng)該明確指定javaType 來(lái)確保所需行為。resultMap:一個(gè)可以映射聯(lián)合嵌套結(jié)果集到一個(gè)適合的對(duì)象視圖上的ResultMap。這是一個(gè)替代的方式去調(diào)用另一個(gè)select語(yǔ)句。
association查詢與主查詢一般寫在一個(gè)文件中,如下:
第一種:不另外寫查詢語(yǔ)句
(1)把返回的屬性提取出來(lái)
<association property="product" resultMap="ResultMapWithBLOBs"/>
(2)糅合在一起
<association property="category" javaType="com.provider.biz.model.auto.TCategory">
<id column="category_id" property="categoryId" jdbcType="INTEGER"/>
<result column="c_name" property="name" jdbcType="VARCHAR"/>
<result column="image_url" property="imageUrl" jdbcType="VARCHAR"/>
<result column="c_status" property="status" jdbcType="CHAR"/>
</association>
第二種:另外寫查詢語(yǔ)句
<resultMap id="testMap" type="xxx" >
<id property="id" column="id" jdbcType="DECIMAL" />
<result property="startDateStr" column="startDateStr" jdbcType="VARCHAR" />
<result property="endDateStr" column="endDateStr" jdbcType="VARCHAR" />
<association property="tableEntity" column="table_a_id" select="selectById"></association>
</resultMap>
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from Table_XXX
where a_id=#{aId} (注:這里的aId即為上述傳的參數(shù)值table_a_id,只傳一個(gè)參數(shù)時(shí)名稱可以不一樣)
</select>
第三種:與collection一樣
提到外面其他的dao接口中,此處省略…
補(bǔ)充:
association的使用要特別注意下,數(shù)據(jù)映射時(shí)會(huì)出現(xiàn)異常,再一次的項(xiàng)目中就出現(xiàn)過實(shí)際有5條數(shù)據(jù),但返回到service層時(shí)只有1條數(shù)據(jù),這一條數(shù)據(jù)各個(gè)部分的信息還不對(duì)應(yīng),通過打印mysql執(zhí)行語(yǔ)句和逐條排查,發(fā)現(xiàn)是association標(biāo)簽的問題;
切記,association使用時(shí)<resultMap id="testMap" type="xxx" >中一定要有 <id>標(biāo)簽,否則會(huì)出現(xiàn)返回的數(shù)據(jù)與實(shí)際查詢結(jié)果不一致的問題
多參數(shù)傳遞
當(dāng)需要傳遞多個(gè)值時(shí),寫法如下:
column= “{prop1=col1,prop2=col2}接收的類型為 parameterType=“java.util.Map”,這里的參數(shù)使用時(shí)名稱要保持一致;
例:
<resultMap id="testMap" type="xxx" >
<id property="id" column="id" jdbcType="DECIMAL" />
<result property="startDateStr" column="startDateStr" jdbcType="VARCHAR" />
<result property="endDateStr" column="endDateStr" jdbcType="VARCHAR" />
<association property="tableEntity" column="{reqId=id,endDate=endDateStr,startDate=startDateStr}" select="selectById"> </association>
</resultMap>
<select id="selectById" parameterType="java.util.Map" resultMap="BaseResultMap">
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Mybatis中collection和association的使用區(qū)別詳解
- mybatis利用association或collection傳遞多參數(shù)子查詢
- Mybatis之a(chǎn)ssociation和collection用法
- 在Mybatis中association標(biāo)簽多層嵌套的問題
- mybatis中一對(duì)一關(guān)系association標(biāo)簽的使用
- MyBatis中association的基本使用方法
- mybatis的association傳遞參數(shù)問題示例
- MyBatis的collection和association的使用解讀
- mybatis中association標(biāo)簽的使用解讀
- MyBatis使用嵌套查詢collection和association的實(shí)現(xiàn)
- Mybatis的association使用子查詢結(jié)果錯(cuò)誤的問題解決
相關(guān)文章
SpringBoot Redisson 集成的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot Redisson 集成的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
解決spring-boot 打成jar包后 啟動(dòng)時(shí)指定參數(shù)無(wú)效的問題
這篇文章主要介紹了解決spring-boot 打成jar包后 啟動(dòng)時(shí)指定參數(shù)無(wú)效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot整合Drools規(guī)則引擎動(dòng)態(tài)生成業(yè)務(wù)規(guī)則的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Drools規(guī)則引擎動(dòng)態(tài)生成業(yè)務(wù)規(guī)則的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

