mybatis中collection通过两个参数确定关联的写法

mybatis中collection的column传入多个参数值
项目中在使用association和collection实现一对一和一对多关系时需要对关系中结果集进行筛选,如果使用懒加载模式,即联合使用select标签时,主sql和关系映射里的sql是分开的,查询参数传递成为问题。

propertydescription
column数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的参数名称相同。注意: 在处理组合键时,您可以使用column=”{prop1=col1,prop2=col2}”这样的语法,设置多个列名传入到嵌套查询语句。这就会把prop1和prop2设置到目标嵌套选择语句的参数对象中。

1、多个条件一对多查询
Mapper.xml文件配置
在这里插入图片描述
在这里插入图片描述
实体类配置
在这里插入图片描述
代码示例:

<resultMap id="findCountryCityAddressMap" type="map">
 <result property="country" column="country"/>
 <collection property="cityList"
    column="{cityId=city_id,adr=addressCol, dis=districtCol}" //adr作为第二个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, //为传入查询条件,构造虚拟列,虚拟列为查询条件参数值
  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>

补充:
association 与 collection的区别(网上的解释):

association 一对一
collection 一对多

public class Student{
private String name;
//学生父亲
private Parent p;
//学生所选课程
private List c;
}

查询学生时关联查询其父信息用association:用于映射关联查询单个对象的信息
关联查询所选课程时用collection:对关联查询到的多条记录映射到集合对象中

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis collection 操作可以通过传递多个参数来实现。常见的情况是,我们需要在查询传递一个集合参数和一个字符串参数。例如,我们需要查询某个用户的订单信息,其用户 ID 存在一个集合,订单状态存在一个字符串参数。 在 MyBatis ,我们可以使用 Map 类型的参数来传递多个参数。具体实现步骤如下: 1. 在 Mapper.xml 文件,定义一个 Map 类型的参数,例如: ``` <parameterMap type="java.util.Map" id="orderParameter"> <parameter property="userIds" javaType="java.util.List" /> <parameter property="status" javaType="java.lang.String" /> </parameterMap> ``` 其,userIds 是一个 List 类型的参数,用于传递用户 ID 集合;status 是一个 String 类型的参数,用于传递订单状态。 2. 在查询语句使用 collection 操作,例如: ``` <select id="getOrderInfo" parameterMap="orderParameter" resultType="Order"> SELECT * FROM order WHERE user_id IN <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")"> #{item} </foreach> AND status = #{status} </select> ``` 其,parameterMap 属性指定了我们刚刚定义的参数 Map,collection 属性指定了要遍历的集合参数,item 属性指定了集合每个元素的变量名,open、separator、close 属性定义了集合元素的分隔符,在 SQL 语句会被拼接成 IN 子句。 通过这样的方式,我们可以在 MyBatis 很方便地传递多个参数,实现复杂的查询操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值