1. 程式人生 > >MyBatis-自定義結果映射規則

MyBatis-自定義結果映射規則

mybatis 進行 last rtm 對象 gen tor 規則 cti

1、自定義結果集映射規則

  ①查詢

    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById"  resultMap="MySimpleEmp">
        select * from tbl_employee where id=#{id}
    </select>

  ②結果集映射

<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
        <!--
指定主鍵列的封裝規則 id定義主鍵會底層有優化; column:指定哪一列 property:指定對應的javaBean屬性 --> <id column="id" property="id"/> <!-- 定義普通列封裝規則 --> <result column="last_name" property="lastName"/> <!-- 其他不指定的列會自動封裝:我們只要寫resultMap就把全部的映射規則都寫上。
--> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap>

  type:自定義規則的Java類型

  id:唯一id方便引用

2、聯合查詢:

<!--  public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="MyDifEmp">
SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>

  ①級聯屬性封裝結果集

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>

  ②使用association定義關聯的單個對象的封裝規則

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        
        <!--  association可以指定聯合的javaBean對象
        property="dept":指定哪個屬性是聯合的對象
        javaType:指定這個屬性對象的類型[不能省略]
        -->
        <association property="dept" javaType="com.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>

3、分步查詢(association)

     <!--  public Employee getEmpByIdStep(Integer id);-->
     <select id="getEmpByIdStep" resultMap="MyEmpByStep">
         select * from tbl_employee where id=#{id}
   </select>

    <!-- 使用association進行分步查詢:
        1、先按照員工id查詢員工信息
        2、根據查詢員工信息中的d_id值去部門表查出部門信息
        3、部門設置到員工中;
     -->
     
     <!--  id  last_name  email   gender    d_id   -->
     <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
         <id column="id" property="id"/>
         <result column="last_name" property="lastName"/>
         <result column="email" property="email"/>
         <result column="gender" property="gender"/>
         <!-- association定義關聯對象的封裝規則
             select:表明當前屬性是調用select指定的方法查出的結果
             column:指定將哪一列的值傳給這個方法
             流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,並封裝給property指定的屬性
          -->
         <association property="dept" 
             select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
             column="d_id">
         </association>
     </resultMap>

4、嵌套結果集查詢(collection)

    <!-- public Department getDeptByIdPlus(Integer id); -->
    <select id="getDeptByIdPlus" resultMap="MyDept">
        SELECT d.id did,d.dept_name dept_name,
                e.id eid,e.last_name last_name,e.email email,e.gender gender
        FROM tbl_dept d
        LEFT JOIN tbl_employee e
        ON d.id=e.d_id
        WHERE d.id=#{id}
    </select>

<!--嵌套結果集的方式,使用collection標簽定義關聯的集合類型的屬性封裝規則  -->
    <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            collection定義關聯集合類型的屬性的封裝規則 
            ofType:指定集合裏面元素的類型
        -->
        <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
            <!-- 定義這個集合中元素的封裝規則 -->
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>

5、分段查詢(collection)

<!-- public Department getDeptByIdStep(Integer id); -->
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id,dept_name from tbl_dept where id=#{id}
    </select>

<!-- collection:分段查詢 -->
    <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
            column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>

擴展:

  將多列的值封裝map傳遞 column="{key1=column1,key2=column2}"

  fetchType="lazy":表示使用延遲加載;

      - lazy:延遲
      - eager:立即

6、鑒別器(discriminator) 

  mybatis可以使用discriminator判斷某列的值,然後根據某列的值改變封裝行為

  封裝Employee:

  如果查出的是女生:就把部門信息查詢出來,否則不查詢;
  如果是男生,把last_name這一列的值賦值給email;

     <!--  public Employee getEmpByIdStep(Integer id);-->
     <select id="getEmpByIdStep" resultMap="MyEmpDis">
         select * from tbl_employee where id=#{id}
     </select>

<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpDis">
         <id column="id" property="id"/>
         <result column="last_name" property="lastName"/>
         <result column="email" property="email"/>
         <result column="gender" property="gender"/>
         <!--
             column:指定判定的列名
             javaType:列值對應的java類型  -->
         <discriminator javaType="string" column="gender">
             <!--女生  resultType:指定封裝的結果類型;不能缺少。/resultMap-->
             <case value="0" resultType="com.atguigu.mybatis.bean.Employee">
                 <association property="dept" 
                     select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                     column="d_id">
                 </association>
             </case>
             <!--男生 ;如果是男生,把last_name這一列的值賦值給email; -->
             <case value="1" resultType="com.atguigu.mybatis.bean.Employee">
                 <id column="id" property="id"/>
                 <result column="last_name" property="lastName"/>
                 <result column="last_name" property="email"/>
                 <result column="gender" property="gender"/>
             </case>
         </discriminator>
     </resultMap>

MyBatis-自定義結果映射規則