首页 > 其他分享 >Mybatis中ResultMap的嵌套查询

Mybatis中ResultMap的嵌套查询

时间:2024-04-14 17:25:40浏览次数:11  
标签:ResultMap 查询 嵌套 tid Mybatis where id select

要点:

  • 多对一:查询的多个对象中有一个属性包含一个复杂对象,使用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

标签:ResultMap,查询,嵌套,tid,Mybatis,where,id,select
From: https://www.cnblogs.com/isme-zjh/p/18134376

相关文章

  • 在MyBatis中,可以使用以下动态SQL标签来编写灵活的SQL语句
    一、<if>:条件判断标签,用于在SQL语句中添加条件判断。通过判断给定的条件是否成立,决定是否包含相应的SQL片段。示例:<selectid="getUserList"resultType="User">SELECT*FROMuser<where><iftest="username!=null">ANDusername=#{userna......
  • 内联和嵌套命名空间
    在开发大型的项目时,往往会有很多人参与协同开发,划分成各个小组负责不同的模块,模块之间相对独立。代码中会定义很多的类名、函数名、模板名,甚至一些全局变量,如果不对这些名称加以规范,很容易造成名字的冲突,因为默认情况下这些名字都是全局名字,这种情况也称之为命名空间污染。为了避......
  • java: 无法访问org.mybatis.spring.annotation.MapperScan、类文件具有错误的版本 61.
    使用的Mybatis-spring依赖的版本3.0.1太高,将版本改为2.2.2<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><depende......
  • mybatis怎么实现insert into多个数据-oracle数据库
    第一种<insertid="insertBatch"> INSERTALL <foreachcollection="list"item="user"separator=""close="SELECT*FROMdual"index="index"> INTOLY_TEST(id,name,age)VALUES(#{user......
  • MyBatis动态SQL
    MyBatis动态SQL动态SQL简介动态SQL是MyBatis的强大特性之一。如果你使用过JDBC或其它类似的框架,你应该能理解根据不同条件拼接SQL语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态SQL,可以彻底摆脱这种痛苦。使用动态......
  • Mybatis中update语句的写法详解
    mybatis中有很多时候是需要写到update语句的,update语句可以直接写成固定字段也可以拼接成动态的sqlmybatis的xml更新语句中  update标签可以直接写如下的update语句(方式一)<updateid="updateNoticeTest">updateoutbound_notice_testsetnotice_state=......
  • 美团二面:为什么不推荐使用 MyBatis 二级缓存?大部分人都答不上来!
    为了增加查询的性能,MyBatis提供了二级缓存架构,分为一级缓存和二级缓存。这两级缓存最大的区别就是:一级缓存是会话级别的,只要出了这个SqlSession,缓存就没用了。而二级缓存可以跨会话,多个会话可以使用相同的缓存!一级缓存使用简单,默认就开启。二级缓存需要手动开启,相对复杂,而且要......
  • 25.再次整理mybatis坑
    前面就没有整理好mybatis也是因为跳过这个直接去学的springboot也导致没有更深层次理解也是逢坑踩坑一下我maven版本为2.2.5.RELEASE1导入mybatis和mysql驱动依赖如果mybatis加载不出来降低版本参考:https://blog.csdn.net/S852509769/article/details/134456125<!-......
  • SSM整合(Spring、SpringMVC、Mybatis)
    第一步:新建一个Webx项目第二步:导入相关依赖包第三步:配置Spring核心配置文件第四步:配置SpringMVC核心配置文件第五步:配置web.xml文件......
  • 人工智能_大模型030_大模型开发框架003_Semantic Kernel中Native Function嵌套调用_SK
    ###4.2、NativeFunction嵌套调用(选)**注意:**NativeFunction的嵌套调用,本质上就是函数嵌套。官方给的写法是在Kernel的设计思想下的实现,通过Kernel来获取函数并执行,观感上较为晦涩。实际开发中,可以根据个人对SK内核与设计理念的理解,自行选择使用以下写法,或使用普......