zoukankan      html  css  js  c++  java
  • MyBatis(四)映射文件 之 分步查询与懒加载

    一、分步查询

      1、分步查询

        在前面已经使用了 association 和 collection 的分步查询。

        <resultMap type="Dept" id="deptMapStep">
             <id column="did" property="did"/>
             <result column="dname" property="dname"/>
             <collection property="emps"  select="com.mybatis.mapper.EmpDeptMapper.getEmpListByDid" column="did"></collection>
             <!--
                  property:用来操作的属性
                  select:分步查询的SQL的id,即接口的全限定名.方法名或namespace.SQL的id
                  column:分步查询的条件,注意:此条件必须是从数据库查询过得(即调用的select方法的参数)
              -->
         </resultMap>
         
         <!-- Dept getOnlyDeptByDid(String did); -->
         <select id="getOnlyDeptByDid"  resultMap="deptMapStep">
             select did,dname from dept where did = #{did}
         </select>
         
         <!-- List<Emp> getEmpListByDid(String did); -->
         <select id="getEmpListByDid" resultType="Emp">
             select eid,ename,age,sex from emp where did =  #{did}
         </select>

      2、分步查询-多列值封装 map 传递

        对于上面的分步查询来说,需要给调用的查询方法传递一个 did 的参数,可以使用 column 属性来传递参数,而且这个参数是作为分步条件的参数,还必须是从数据库中查询出来的。
        如果对于分步查询需要多列值的传递呢?
        (1)如果分步查询时,需要传递给调用的查询中多个参数,则需要将多个参数封装成  Map 来进行传递,语法如:{key1=value,key2=value2...}
        (2)在所调用的查询方法,取值时就要参考 Map 的取值方式,需要严格的按照封装的 map 时所用的 key 来取值。

        

         如:

    二、懒加载

      1、在 <association> 和 <collection> 标签中都可以设置 fetchType,指定本次查询是否非要使用延迟加载。
        默认为 fetchType="lazy",如果本次的查询不想使用延迟加载,则可设置为 fetchType="eager";
     
      2、fetchType 可以灵活的设置查询是否需要使用延迟加载,而不需要因为某个查询不想使用延迟加载而将全局的延迟加载设置关闭。 
        使用 fetchType 属性来单独指定某一个分步查询的延迟加载。
         <resultMap type="Dept" id="deptMapStep">
             <id column="did" property="did"/>
             <result column="dname" property="dname"/>
             <collection property="emps"  select="com.mybatis.mapper.EmpDeptMapper.getEmpListByDid" column="{id=did}" fetchType="eager"></collection>
             <!--
                  property:用来操作的属性
                  select:分步查询的SQL的id,即接口的全限定名.方法名或namespace.SQL的id
                  column:分步查询的条件,注意:此条件必须是从数据库查询过得(即调用的select方法的参数)
              -->
         </resultMap>
         
         <!-- Dept getOnlyDeptByDid(String did); -->
         <select id="getOnlyDeptByDid"  resultMap="deptMapStep">
             select did,dname from dept where did = #{did}
         </select>
         
         <!-- List<Emp> getEmpListByDid(String did); -->
         <select id="getEmpListByDid" resultType="Emp">
             select eid,ename,age,sex from emp where did =  #{id}
         </select>
      注意:association或者collection标签的 fetchType=eager/lazy 可以覆盖全局的延迟加载策略,指定立即加载( eager) 或者延迟加载( lazy)
     
     
  • 相关阅读:
    二分图 洛谷P2055 [ZJOI2009]假期的宿舍
    并查集 洛谷P1640 [SCOI2010]连续攻击游戏
    贪心 洛谷P2870 Best Cow Line, Gold
    贪心 NOIP2013 积木大赛
    快速幂 NOIP2013 转圈游戏
    倍增LCA NOIP2013 货车运输
    树形DP 洛谷P2014 选课
    KMP UVA1328 Period
    动态规划入门 BZOJ 1270 雷涛的小猫
    KMP POJ 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/niujifei/p/15238900.html
Copyright © 2011-2022 走看看