详解mybatis中association和collection的column传入多个参数问题
项目中在使用association和collection实现一对一和一对多关系时需要对关系中结果集进行筛选,如果使用懒加载模式,即联合使用select标签时,主sql和关系映射里的sql是分开的,查询参数传递成为问题。
mybatis文档:
SELECT ct.country, ci.city_id, IFNULL(#{addressQuery},'')addressCol,//为传入查询条件,构造虚拟列,虚拟列为查询条件参数值 IFNULL(#{districtQuery},'')districtCol FROM countryct LEFTJOINcityciONct.country_id=ci.country_id ORDERBYct.country_id SELECT ci.city, ads.address, ads.district FROM ( SELECT city, city_id FROM cityci WHERE ci.city_id=#{cityId} )ci LEFTJOINaddressadsONads.city_id=ci.city_id andads.addressRegExp#{adr} ads.districtRegexp#{dis}
测试文件:
@Test
publicvoidfindCountryCityAddressTest()throwsJsonProcessingException{
Mapparam=newHashMap<>();
param.put("addressQuery","1168");
List
测试结果:
[
{
"country":"Afghanistan",
"cityList":[{
"city":"Kabul",
"addressList":[{
"address":"1168NajafabadParkway",
"district":"Kabol"
}
]
}
],
"city_id":251
},
{
"country":"Algeria",
"cityList":[],
"city_id":59
}
]
可以看到,确实将查询条件通过column参数传入到第二个sql中,并执行成功
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。