1. 程式人生 > >mybatis3.2.7應用_高級映射(一對一、一對多、多對多)

mybatis3.2.7應用_高級映射(一對一、一對多、多對多)

mybatis3 單個 所有 由於 單表 myba 用戶 記錄 text

1. 一對一查詢

需求:查詢訂單信息,關聯查詢創建訂單的用戶信息

1.1 使用resultType實現

1.1.1 sql語句

   確定查詢的主表:訂單表
  確定查詢的關聯表:用戶表
  關聯查詢:使用外連接,還是內連接??

       --由於orders表中有一個外鍵(user_id),通過外鍵關聯查詢用戶表只能查詢出一條記錄,可以使用內連接

select t.*, 
         u.username,  u.sex,  u.address
  from orders t, user u
 where t.user_id = u.id

1.1.2 創建pojo

   將上邊sql查詢結果映射到pojo中,pojo中必須包含所有查詢列名。
原始的Orders不能映射全部字段,需要新創建pojo。創建一個pojo繼承包括查詢字段較多的po類。

//通過此類映射訂單和用戶和用戶查詢的結果,讓此類繼承包括字段較多的pojo類
public class OrdersCustom extends Orders{
     //添加用戶屬性
    //u.username, u.sex, u.address
     private String username;
     private String sex;
     private String address;
}

1.1.3 mapper.xml

<!-- 查詢訂單,關聯查詢用戶信息 -->
<select id="findOrderUser" resultType="cn.itcast.mybatis.po.OrdersCustom">
        select t.*, u.username, u.sex, u.address
         from orders t, user u
        where t.user_id = u.id
</select>

1.1.4 mapper.java

public List<OrdersCustom> findOrderUser() throws
Exception;

1.2 使用resultMap實現
1.2.1 sql語句
  同resultType實現的sql
1.2.2 使用resultType映射的思路
使用resultType將查詢結果中的訂單信息映射到Orders對象中,在Orders對象中添加user屬性,將關聯查詢出來的用戶信息映射到Orders對象中user屬性中。
需要Orders類中添加user屬性。

public class Orders{
     
     private Integer id;
     private Integer userId;
     private String number;
     private Date createTime;
     private String note;
     
     //用戶信息
    private User user;
}

1.2.3 mapper.xml 定義resultMap(一對一使用association來映射用戶信息)

    <!-- 訂單查詢關聯查詢用戶信息 的 resultMap
           將整個查詢的結果映射到cn.itcast.mybatis.po.Orders中
    -->
    <resultMap type="cn.itcast.mybatis.po.Orders" id="ordersUserResultMap">
         <!-- 配置映射的訂單信息-->
         <!-- id: 指定查詢列中的唯一標識,訂單信息中的唯一標識,如果有多個列組成唯一標識,配置多個id
                column: 訂單信息中的唯一標識列
            property:訂單信息中的唯一標識列所映射到Orders中的哪個屬性-->
         <id column="id" property="id"/>
         <result column="user_id" property="userId"/>
         <result column="number" property="number"/>
         <result column="createTime" property="createTime"/>
         <result column="note" property="note"/>
     
         <!-- 配置映射的關聯的用戶信息
           association: 用於映射關聯查詢單個對象的信息
           property: 要將關聯查詢的用戶信息映射到Orders中哪個屬性
           javaType: 指定映射到關聯用戶的pojo類型中-->
         <association property="user" javaType="cn.itcast.mybatis.po.User">
              <!-- id: 關聯查詢用戶的唯一標識
               column: 指定唯一標識用戶信息的列
               property:映射到user的哪個屬性-->
              <id column="user_id" property="id"/>
              <result column="username" property="username"/>
             <result column="sex" property="sex"/>
             <result column="address" property="address"/>
          </association>
    </resultMap>        

1.2.4 mapper.xml statement

<!-- 查詢訂單,關聯查詢用戶信息 -->
<select id="findOrdersUserResultMap" resultMap="ordersUserResultMap">
     select t.*, u.username, u.sex, u.address
      from orders t, user u
    where t.user_id = u.id
</select>

1.2.5 mapper.java

public List<Orders> findOrdersUserResultMap() throws Exception;

1.3 resultType 和 resultMap 實現一對一查詢小結
一對一查詢
  resultType: 使用resultType較為簡單,如果pojo中沒有包括查詢出來的列名,需要增加列名對應的屬性,即可完成映射。
       如果沒有查詢結果的特殊要求,建議使用resultType。
  resultMap: 需要單獨定義resultMap,實現有點麻煩,如果對查詢結果有特殊的要求,使用resultMap可以完成關聯查詢映射到pojo屬性中。
       resultMap可以實現延時加載。而resultType無法實現延時加載。

  

    

mybatis3.2.7應用_高級映射(一對一、一對多、多對多)