1. 程式人生 > 其它 >Java -- MyBatis學習筆記12、聯合查詢

Java -- MyBatis學習筆記12、聯合查詢

1、一對一關聯

以客戶表和客戶詳情表為例,客戶表裡存放基本資訊,客戶詳情表存放詳細資訊。

1.1、提出需求

根據客戶id,查詢客戶資訊並查到詳情表對應的資訊。

  • 表字段以及對應關係

1.2、建立實體類

  • 首先定義客戶實體類

client表中有一個client_info_id欄位,所以在Client類中定義一個clientInfo屬性,用於維護Client和ClientInfo之間的一對一關係,將聯合查詢的結果中的客戶詳情資訊,對映的該屬性當中。

public class Client {
    private int id;
    private String client_name;
    private String client_age;
    private String client_company;
    //客戶詳情
    private ClientInfo clientInfo;
    //getter and setter...
}
  • 定義客戶詳情實體類
public class ClientInfo {
    private int id;
    private String client_hobby;
    private String client_address;
    //getter and setter...
}
  • 定義Dao層介面
public interface ClientDao {
    Client queryClientAndInfo(int id);
}
  • mapper對映檔案
<mapper namespace="com.rg.dao.ClientDao">
    <!--
        方式一:直接聯合查詢、通過association標籤、將查詢到的客戶詳情資訊
                對映到clientInfo屬性當中來。
    -->
    <select id="queryClientAndInfo" resultMap="client_info_map">
        SELECT c.client_name, c.client_age, c.client_company, c.client_info_id,ci.id, ci.client_hobby, client_address
        FROM client as c
                 INNER JOIN client_info as ci on c.client_info_id = ci.id
        where c.id = #{id}
    </select>
    <resultMap id="client_info_map" type="com.rg.bean.Client">
        <id property="id" column="id"/>
        <result property="client_name" column="client_name"/>
        <result property="client_age" column="client_age"/>
        <result property="client_company" column="client_company"/>
        <!--
            型別為:ClientInfo、也就是客戶詳情
        -->
        <association property="clientInfo" javaType="com.rg.bean.ClientInfo">
            <id property="id" column="id"/>
            <result property="client_address" column="client_address"/>
            <result property="client_hobby" column="client_hobby"/>
        </association>
    </resultMap>
    <!--
        方式二:巢狀查詢:通過執行另外一個SQL對映語句來返回預期的複雜型別
    -->
    <select id="queryClientAndInfo" resultMap="client_Info_map">
        select * from client where id = #{id}
    </select>
    <!-- 
        resultMap 可以自定義 sql 的結果和 java 物件屬性的對映關係。
        更靈活的把列值賦值給指定屬性。
        常用在列名和 java 物件屬性名不一樣的情況。
        property:物件屬性名稱
        JavaType:物件屬性型別
        column:資料庫表字段名稱
        select:使用另一個查詢的封裝結果
     -->
    <resultMap id="client_Info_map" type="com.rg.bean.Client">
        <!-- 主鍵欄位使用id  -->
        <id property="id" column="id"/>
        <!-- 非主鍵欄位使用result -->
        <result property="client_name" column="client_name"/>
        <result property="client_age" column="client_age"/>
        <result property="client_company" column="client_company"/>
        <!--通過select、執行另一個sql語句-->
        <association property="clientInfo" column="client_info_id" javaType="com.rg.bean.ClientInfo" select="selectClientInfo"/>
    </resultMap>
    <select id="selectClientInfo" resultType="com.rg.bean.ClientInfo">
        select * from client_info where id = #{id}
    </select>
</mapper>
  • 測試
@Test
public void test01() {
    Client client = clientDao.queryClientAndInfo(1);
    System.out.println(client);
}
  • 結果
Client{id=1, client_name='王經理', client_age='18', client_company='阿里巴巴', clientInfo=ClientInfo{id=1, client_hobby='打籃球', client_address='上海'}}

個人感覺第二種方式更簡潔,首先它不需要寫很長的聯合語句,其次結構更加簡潔明瞭。