mybatis 高级查询


1.oneToOne

< select id = "getClass" parameterType = "int" resultMap = "ClassResultMap" >
          select *
          from
          class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
      </ select >
      < resultMap type = "common.pojo.Classes" id = " ClassResultMap " >
           < id property = "id" column = "c_id" />
           < result property = "name" column = "c_name" />
           < association property = "teacher" javaType = "common.pojo.Teacher" >
               < id property = "id" column = "t_id" />
               < result property = "name" column = "t_name" />
           </ association >
      </ resultMap >

      <!-- 返回方式,子查询 -->
      < select id = "getClass2" parameterType = "int" resultMap = " ClassResultMap2 " >
          select *
          from
          class where c_id=#{id}
      </ select >

      < resultMap type = "common.pojo.Classes" id = "ClassResultMap2" >
           < id property = "id" column = "c_id" />
           < result property = "name" column = "c_name" />
           < association property = "teacher" column = "teacher_id"
               select = "getTeacher" >
           </ association >
      </ resultMap >

      < select id = "getTeacher" parameterType = "int" resultMap = "teacherResultMap" >
          select *
          from teacher where t_id=#{id}
      </ select >
      < resultMap type = "common.pojo.Teacher" id = "teacherResultMap" >
           < id property = "id" column = "t_id" />
           < result property = "name" column = "t_name" />
      </ resultMap >

2.oneToMany
< mapper namespace = "mapping.classesMapper" >
      <!-- 返回方式,resultMap -->
      < select id = "getClass" parameterType = "int" resultMap = "ClassResultMap" >
          select *
          from
          class c,teacher t,student s where c.teacher_id=t.t_id and
          s.class_id=c.c_id and c.c_id=#{id}
      </ select >
      < resultMap type = "common.pojo.Classes" id = "ClassResultMap" >
           < id property = "id" column = "c_id" />
           < result property = "name" column = "c_name" />
           < association property = "teacher" javaType = "common.pojo.Teacher" >
               < id property = "id" column = "t_id" />
               < result property = "name" column = "t_name" />
           </ association >
           < collection property = "students" ofType = "common.pojo.Student" >
               < id property = "id" column = "s_id" />
               < result property = "name" column = "s_name" />
           </ collection >
      </ resultMap >
----------------------------------------------------------------------------------
      <!-- 返回方式,子查询 -->
      < select id = "getClass2" parameterType = "int" resultMap = "ClassResultMap2" >
          select *
          from
          class where c_id=#{id}
      </ select >

      < resultMap type = "common.pojo.Classes" id = "ClassResultMap2" >
           < id property = "id" column = "c_id" />
           < result property = "name" column = "c_name" />
           < association property = "teacher" column = "teacher_id"
               select = "getTeacher" >
           </ association >
           < collection property = "students" ofType = "common.pojo.Student"
               column = "c_id" select = "getStudent" ></ collection >
             这里的column是说明传哪个值给子查询
      </ resultMap >

      < select id = "getTeacher" parameterType = "int" resultType = "common.pojo.Teacher" >
          select
          t_id id,t_name
          name
          from teacher where t_id=#{id}
      </ select >

      < select id = "getStudent" parameterType = "int" resultType = "common.pojo.Student" >
          select
          s_id id,s_name
          name
          from student where class_id=#{id}
      </ select >

</ mapper >

MyBatis中使用collection标签来解决一对多的关联查询,ofType属性指定集合中元素的对象类型。

MyBatis中,高级查询可以使用延迟加载机制来实现。延迟加载是将数据加载时机推迟,例如推迟嵌套查询的执行时机。通过延迟加载,可以先查询主表,按需实时做关联查询,返回关联表结果集,提高效率。 在MyBatis中,实现关联查询有两种不同的方式:嵌套Select查询和嵌套结果映射。嵌套Select查询是通过执行另外一个SQL映射语句来加载期望的复杂类型,而嵌套结果映射则使用嵌套的结果映射来处理连接结果的重复子集。 对于关联结果映射和其他类型的映射,工作方式类似。需要指定目标属性名以及属性的javaType,大多数情况下MyBatis可以推断出来,如果需要的话,还可以设置JDBC类型。如果想要覆盖获取结果值的过程,可以设置类型处理器。 综上所述,通过延迟加载机制和适当的关联查询方式,MyBatis可以实现高级查询功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybatis高级查询](https://blog.csdn.net/weixin_37650458/article/details/96587906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MyBatis高级查询](https://blog.csdn.net/qq_66991094/article/details/127147576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值