返回顶部

Mybatis中ResultMap的嵌套查询

要点:

  • 多对一:查询的多个对象中有一个属性包含一个复杂对象,使用association标签嵌套
  • 一对多:查询的单个对象中的一个属性包含多个其他对象,使用collection标签嵌套
  • javaType为实体类中属性的类型,为常用基本数据类型时可以省略
  • ofType则是用来指定到List或集合中的实体类类型,泛型中的约束类型
  • 按照查询嵌套时,即查询出来后再嵌套,查询语句一般是由两个查询语句组成,类似子查询
  • 按照结果嵌套时,即查询时整体嵌套,查询语句一般为联合查询

注意:以下实体类都已使用了别名

1.多对一:使用association

  //查询所有学生以及对应老师的信息
    List<Student>  selectAllStu2();
    List<Student>  selectAllStu3();

按照查询嵌套:子查询

 <!--多对一  按照查询嵌套处理  子查询-->
 
     <!--先查询所有学生信息-->
    <select id="selectAllStu3" resultMap="StudentTeacher2">
        select * from student
    </select>
 
    <!--编写映射关系-->
    <resultMap id="StudentTeacher2" type="student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
         <!--复杂属性 查询结果为单个老师对象,所以使用association,
        其中property为student类中对应的老师属性字段,select指定子查询语句,
        因为子查询中需要一个id的参数,所有使用coumn,
        将student查询出的结果字段中的tid传递给子查询语句的id参数
        javatype由于老师查询对象返回的是一个老师类实体对象,所以类型为老师类-->
        <association property="teacher" select="selectAllTea" column="tid" javaType="teacher"/>
    </resultMap>
        
    <!--根据指定id查老师对象-->
    <select id="selectAllTea" resultType="teacher">
        select * from teacher where id=#{tid};
    </select>

按照结果嵌套:联合查询

<!--多对一 按照结果嵌套处理 联合查询-->
 
    <!--联合查询所有需要的信息-->
    <select id="selectAllStu2" resultMap="StudentTeacher">
        select s.id sid,s.name sname,t.name tname,t.id tid 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"/>
        <!--查询结果为单个老师对象,所以方法中返回的类型为老师对象,
            即javaType为老师对象,然后在对象里编写其对应的老师的映射关系即可-->
        <association property="teacher" javaType="teacher">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

2.一对多:使用collection

   Teacher selectTeacher(@Param("tid") int id);//查询一个老师的多个学生
   Teacher selectTeacher2(@Param("tid") int id);

按照查询嵌套:子查询

  <!--一对多 按查询嵌套处理 子查询-->
    <!--首先根据id查询指定老师的信息-->
    <select id="selectTeacher2" resultMap="TeacherStudent2">
         select * from teacher where id=#{tid};
    </select>
    <!--编写结果集映射-->
    <resultMap id="TeacherStudent2" type="teacher">
        <!--此处由于属性和字段名一致所以下面两行可以省略-->
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--由于查询老师中有一个属性为所有学生对象集合,所一使用collection标签进行嵌套,
        方法的返回值是List,所以javaType使用ArrayList来接收,而list中泛型约束类型放的
        是student对象所以使用ofType为student来进行映射,使用select进行子查询,并用column
        将查询出的id字段传给子查询需要的id-->
        <!--此处javaType可以省略-->
        <collection property="student" javaType="ArrayList" ofType="student" select="selectStu" column="id"/>
    </resultMap>
    <!--子查询-->
    <select id="selectStu" resultType="student">
         select * from student where tid=#{tid};
    </select>

按照结果嵌套:联合查询

<!--一对多  按结果嵌套查询  多表查询-->
    <!--联合查询所有需要的信息-->
    <select id="selectTeacher" resultMap="TeacherStudent">
        select  t.id  tid,t.name  tname,s.id  sid,s.name  sname from student s,teacher t where t.id=s.tid and t.id=#{tid};
    </select>
    <!--编写结果集映射-->
    <resultMap id="TeacherStudent" type="teacher">
        <result column="tid" property="id"/>
        <result column="tname" property="name"/>
        <!--因为查询的老师实体类字段为studnet,所以属性名为student,查询出的映射对象、
            是student类,所以使用ofType指定为student类-->
        <!--此处加上javaType也能正常运行,进行了省略应该-->
        <collection property="student" ofType="student">
            <result column="sid" property="id"/>
            <result column="sname" property="name"/>
        </collection>
    </resultMap>
  • 多对一:查询的多个对象中有一个属性包含一个复杂对象,使用association标签嵌套

 
 
原文链接 :https://blog.csdn.net/weixin_54158370/article/details/120612127
posted @ 2024-04-14 17:12  fen斗  阅读(45)  评论(0编辑  收藏  举报