Mybatis复杂查询

这篇博客详细介绍了MyBatis中如何进行复杂查询,包括关联的嵌套select查询和嵌套结果映射。示例展示了如何通过resultMap配置来实现对象关联和集合映射,例如将学生信息与对应的教师信息关联,以及在查询结果中直接嵌套显示教师详情。注意property属性应与实体类属性匹配。
摘要由CSDN通过智能技术生成

mybatis复杂查询

关联的嵌套select查询

    <select id="getStudentList" resultMap="studentTeacher">
        select * from school.student
    </select>
    <resultMap id="studentTeacher" type="Student">
        <result property="id" column="id"></result>
        <result property="studentName" column="studentName"></result>
        <result property="password" column="password"></result>
        <result property="teacherId" column="teacherId"></result>
        <!--        复杂的属性
                    对象:association
                    集合:collection
        -->
        <association property="teachers" column="teacherId" javaType="Teacher" select="getTeacherById"/>
    </resultMap>

关联的嵌套结果映射

<select id="getStudentList2" resultMap="studentResult">
        select student.id sid,student.studentName sname,student.teacherId tid,teacher.teacherName tname from student,teacher where student.teacherId=teacher.id
    </select>
    <resultMap id="studentResult" type="Student">
        <result property="id" column="sid"></result>
        <result property="studentName" column="sname"></result>
        <result property="teacherId" column="tid"></result>
        <association property="teachers" column="tid" javaType="Teacher" resultMap="teacherResult"/>
    </resultMap>
    <resultMap id="teacherResult" type="Teacher">
        <result property="teacherName" column="tname"/>
    </resultMap>

以上需要注意的是property属性的值应是实体类的属性名一致
如果属性是对象,则使用assocation,如果属性的对象集合,则使用collection

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值