mybatis中一个resultMap 包含多个association,一个类对应多个类属性写法

经过查询得知一个自定义类中包含多个自定义类是可以写多个association进行映射的,如下:

//实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Detail {
    private String id;
    private String title;
    private String description;
    private String student_id;
    private String teacher_id;
    private String answer;
    private Date create_date;
    //下面两个是自定义类
    private Teacher teacher;
    private Student student;
    
}

<!--  一个resultMap 包含多个association  -->
	<select id="querryAll" resultMap="res1">
        select b.*,rn
        from
         (select d.id did,d.title,d.description,d.answer,d.create_date,t.id tid,t.realname realname,s.id sid,s.name sname,ROWNUM rn
            from IN_DETAIL d left join in_teacher t
            on d.teacher_id=t.id
            left join IN_STUDENT s
            on d.STUDENT_ID=s.ID
             ORDER BY did desc
        ) b
        where rn between #{begin} and #{end}
    </select>
    <resultMap id="res1" type="Detail">
        <id column="did" property="id"></id>
        <result column="title" property="title"></result>
        <result column="description" property="description"></result>
        <result column="answer" property="answer"></result>
        <result column="create_date" property="create_date"></result>
        <association property="teacher" javaType="Teacher">
            <id column="tid" property="id"></id>
            <result column="realname" property="realname"></result>
        </association>
        <association property="student" javaType="Student">
            <id column="sid" property="id"></id>
            <result column="sname" property="name"></result>
        </association>
    </resultMap>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatisResultMap可以用于将查询结果映射到Java对象,而一个ResultMap可以对应多个Collection。 在使用MyBatis进行查询时,有时候我们需要将多个关联的对象一起查询出来并映射到Java对象。这时候,我们可以使用ResultMap的collections属性来实现。 首先,我们需要在ResultMap定义好需要映射的属性和关联对象的映射关系。然后,在需要映射的属性上使用collection元素,并通过column属性指定关联对象的外键字段,使用ofType属性指定关联对象的型。 接下来,我们可以在SQL语句使用JOIN操作将多个表关联起来,并将查询结果映射到对应Java对象。在配置多个Collection映射时,可以使用外键字段作为关联条件来进行关联查询,然后将关联查询的结果映射到对应的Collection属性。 例如,假设我们有两个实体:User和Order,一个用户可以有多个订单。我们可以定义一个ResultMap,将查询结果映射到User对象,并通过collections属性将订单集合映射到User对象的orders属性。 示例代码如下: ``` <resultMap id="userResultMap" type="User"> <id property="id" column="id" /> <result property="name" column="name" /> ... <collection property="orders" ofType="Order" column="user_id"> <id property="orderId" column="order_id" /> <result property="orderName" column="order_name" /> ... </collection> </resultMap> ``` 在SQL语句,我们可以使用JOIN操作将User表和Order表关联起来,并查询出对应的用户和订单信息。最后,通过MyBatis将查询结果映射到User对象,User对象的orders属性包含该用户的所有订单。 总结来说,MyBatisResultMap可以实现一个ResultMap对应多个Collection的功能,通过配置collection元素可以将多个关联对象的集合映射到Java对象对应属性上。这为我们处理复杂的查询结果提供了便利。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值