詳解mybatis中association和collection的column傳入多個(gè)參數(shù)問題
項(xiàng)目中在使用association和collection實(shí)現(xiàn)一對一和一對多關(guān)系時(shí)需要對關(guān)系中結(jié)果集進(jìn)行篩選,如果使用懶加載模式,即聯(lián)合使用select標(biāo)簽時(shí),主sql和關(guān)系映射里的sql是分開的,查詢參數(shù)傳遞成為問題。
mybatis文檔:
| property | description |
|---|---|
| column | 數(shù)據(jù)庫的列名或者列標(biāo)簽別名。與傳遞給resultSet.getString(columnName)的參數(shù)名稱相同。注意: 在處理組合鍵時(shí),您可以使用column=“{prop1=col1,prop2=col2}”這樣的語法,設(shè)置多個(gè)列名傳入到嵌套查詢語句。這就會把prop1和prop2設(shè)置到目標(biāo)嵌套選擇語句的參數(shù)對象中。 |
<resultMap id="findCountryCityAddressMap" type="map">
<result property="country" column="country"/>
<collection property="cityList"
column="{cityId=city_id,adr=addressCol, dis=districtCol}" //adr作為第二個(gè)sql查詢條件key,即prop1屬性
ofType="map" //addressCol即為虛擬列名
javaType="java.util.List" select="selectAddressByCityId"/>
</resultMap>
<resultMap id="selectAddressByCityIdMap" type="map">
<result property="city" column="city"/>
<collection property="addressList" column="city" ofType="map" javaType="java.util.List">
<result property="address" column="address"/>
<result property="district" column="district"/>
</collection>
</resultMap>
<select id="findCountryCityAddress" resultMap="findCountryCityAddressMap">
SELECT
ct.country,
ci.city_id,
IFNULL(#{addressQuery},'') addressCol, //為傳入查詢條件,構(gòu)造虛擬列,虛擬列為查詢條件參數(shù)值
IFNULL(#{districtQuery},'') districtCol
FROM
country ct
LEFT JOIN city ci ON ct.country_id = ci.country_id
ORDER BY ct.country_id
</select>
<select id="selectAddressByCityId" parameterType="java.util.Map" resultMap="selectAddressByCityIdMap">
SELECT
ci.city,
ads.address,
ads.district
FROM
(
SELECT
city,
city_id
FROM
city ci
WHERE
ci.city_id = #{cityId}
) ci
LEFT JOIN address ads ON ads.city_id = ci.city_id
<where>
<if test="adr!=null and adr!=''">
and ads.address RegExp #{adr}
</if>
<if test="dis!=null and dis!=''">
ads.district Regexp #{dis}
</if>
</where>
</select>
測試文件:
@Test
public void findCountryCityAddressTest() throws JsonProcessingException {
Map<String,Object> param = new HashMap<>();
param.put("addressQuery","1168");
List<Map<String, Object>> rs = countryManager.findCountryCityAddress(param);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
System.out.println(writer.writeValueAsString(rs));
}
測試結(jié)果:
[
{
"country": "Afghanistan",
"cityList": [{
"city": "Kabul",
"addressList": [{
"address": "1168 Najafabad Parkway",
"district": "Kabol"
}
]
}
],
"city_id": 251
},
{
"country": "Algeria",
"cityList": [],
"city_id": 59
}
]
可以看到,確實(shí)將查詢條件通過column參數(shù)傳入到第二個(gè)sql中,并執(zhí)行成功
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
springboot web項(xiàng)目打jar或者war包并運(yùn)行的實(shí)現(xiàn)
這篇文章主要介紹了springboot web項(xiàng)目打jar或者war包并運(yùn)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
java使用CountDownLatch等待多線程全部執(zhí)行完成
這篇文章主要為大家詳細(xì)介紹了使用CountDownLatch等待多線程全部執(zhí)行完成,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
這篇文章主要介紹了Google Kaptcha 實(shí)現(xiàn)登錄驗(yàn)證碼(SSM 和 SpringBoot)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記
本文給大家介紹SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11

