Mybatis之多对一的处理

本文详细讲解了如何在Spring Boot中使用MyBatis进行查询,涉及嵌套查询(通过`association`处理复杂属性)和结果嵌套(通过`association`和`result`标签处理关联数据)。实例包括`StudentTeacher`和`StudentTeacher2`的 resultMap,展示了如何连接多个表并获取关联数据。

1.按照查询嵌套进行查询处理

   <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>
    
    <resultMap id="StudentTeacher" type="Student">
<!--        复杂的属性,我们需要单独处理 对象属性使用association 集合属性使用collection-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id}
    </select>

2.按照结果嵌套处理

select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid ,s.name sname ,t.name tname
    from student s,teacher t
    where s.tid=t.id;
</select>

<resultMap id="StudentTeacher2" type="Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

莫荒莫慌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值