mybatis collection 关联查询多个参数

mybatis collection 关联查询多个参数

  • column="{evtId=id,businessType=businessType1}" 描述:evtId是关联查询的参数,businessType也是。
    idresultMap的id,businessType1也是如此
    在这里插入图片描述
       <resultMap id="BaseResultMap" type="com.kingdom.kdum.event.vo.event.EvtToDoListVO">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="businessType1" jdbcType="INTEGER" />
        <result column="businessType2" jdbcType="INTEGER" />
        <collection property="annexeBeforeList" ofType="com.kingdom.kdum.event.mapper.read.event.EvtAnnexeReadMapper"
                    column="{evtId=id,businessType=businessType1}"
                    select="com.kingdom.kdum.event.mapper.read.event.EvtAnnexeReadMapper.findEventPicByEventIdAndBsType">
        </collection>
        <collection property="annexeAfterList" ofType="com.kingdom.kdum.event.mapper.read.event.EvtAnnexeReadMapper"
                    column="{evtId=id,businessType=businessType2}"
                    select="com.kingdom.kdum.event.mapper.read.event.EvtAnnexeReadMapper.findEventPicByEventIdAndBsType">
        </collection>
    </resultMap>
  • 查询:
       <select id="findEventDetailInfo" resultMap="BaseResultMap" parameterType="java.lang.Long">
          SELECT
          <include refid="Base_Column_List"/>,1 as businessType1,2 as businessType2
          FROM
          evt_t_event e
          where e.id = #{id,jdbcType=BIGINT} AND e.db_status = 1
       </select>
    
MyBatis 中可以通过 `collection` 元素来进行关联表的条件查询。 `collection` 元素用于处理一对多的关联关系,它可以用来映射一个 List 或 Array 对象。下面是一个示例的 mapper.xml 文件: ```xml <select id="findUserWithOrders" parameterType="Integer" resultMap="userResultMap"> SELECT u.*, o.id AS order_id, o.order_no AS order_no, o.status AS order_status FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = #{id} ORDER BY o.id </select> <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> <result property="status" column="order_status"/> <!-- other result mappings for Order --> </collection> </resultMap> ``` 这个例子中,我们定义了一个名为 `findUserWithOrders` 的查询语句,它接受一个参数类型为 `Integer` 的对象。查询语句中使用了 LEFT JOIN 连接了 `users` 和 `orders` 表,并在 WHERE 子句中使用了传入的参数 `id` 来限制查询结果只返回指定用户的信息。在结果映射中,我们使用了 `collection` 元素来将一个 `User` 对象中的 `orders` 属性映射为一个 `List<Order>` 对象。 在实际使用中,你需要根据自己的数据表结构和业务逻辑来编写对应的 mapper.xml 文件。你可以参考这个示例,根据自己的情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王子様~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值