MyBatis--resultMap复杂数据查询

使用ResultMap进行一对多的复杂数据查询

在学生与老师这样的多对一的关系,并且学生实体类中存储的是一个老师的对象

//学生实体
@Data
public class Student {
    private  int id;
    private String name;
    private Teacher teacher;//数据库中记录的此字段为tid
}

//教师实体
@Data
public class Teacher {
    private int id;
    private String name;
}

此时查询需要使用resultMap

**方式一:**按照查询嵌套处理

<!--结果集使用resultMap -->
<select id="getStudent" resultMap="studentMap">
    select *  from  student
</select>

<!--resultMap对应学生查询的结果集  类型为学生实体类型 -->
<resultMap id="studentMap" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!--处理Teacher复杂属性  property对应类中属性名称 column对应数据库中字段名称 javaType类型对应Teacher类 select对应查询教师方法 -->
    <association property="teacher" column="tid" javaType="Teacher" select="selectTeacherById"/>
</resultMap>
<!-- 查询教师方法  -->
<select id="selectTeacherById" resultType="teacher">
    select * from teacher where id = #{id}
</select>

在这里插入图片描述

**方式二:**按照返回结果处理

<select id="getStudent2" resultMap="StudentTeacher">

    select s.id sid , s.name sname ,t.name tname
    from  student s ,teacher t
    where s.tid = t.id

</select>

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

图解:
在这里插入图片描述
在老师与学生这样的一对多的关系,并且老师实体类中存储的是一个学生对象的list集合

使用resultMap进行处理

//教师实体类
@Data
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}
//学生实体类
@Data
public class Student {
    private  int id;
    private String name;
    private int tid;
}

查询处理

**方式一:**使用结果集进行处理

Teacher getTeacherStudentById(@Param("tid") int id);
<select id="getTeacherStudentById" resultMap="TeacherStudent">

    select t.id tid,t.name tname,s.name sname,s.id sid
    from teacher t, student s
    where t.id = s.tid and t.id = #{tid}

</select>
<!--
复杂属性处理: collection处理集合 association:处理对象  
javaType:获取指定属性类型
ofType:获取集合中的泛型类型
-->
<resultMap id="TeacherStudent" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    
    <collection property="students" ofType="Student">
        <result property="name" column="sname"/>
        <result property="id" column="sid"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

**方式二:**使用查询嵌套进行处理

 <select id="getTeacherById" resultMap="getTS">

        select * from teacher where id = #{tid}

    </select>

    <select id="getStudent" resultType="student">

        SELECT * FROM  student where tid = #{tid}

    </select>

    <resultMap id="getTS" type="teacher">

        <!--  column中的值为教师表中的教师ID  注意这里写的并不是起的别名tid  也不是学生数据库表中的教师tid   -->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>

    </resultMap>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值