Mybatis關聯映射之association和collection


最近在學習中,遇到一個混淆點,寫篇文章記下來。在Mybatis中使用select語句時,會使用association和collection進行映射。兩者的區別主要是,association用於一對一,即一個實體類對象是另一個實體類對象的屬性;collection用於一對多,例如一個實體類對象里有一個集合作為屬性。

舉一個實例。

一輛車只有一個車主,而車主可以擁有好幾輛車。

汽車實體類:

public class CarBean {
    private int id;
    private String type;
    private int price;
    private ManBean man;

車主實體類:

public class ManBean {
    private int id;
    private String name;
    private LocalDate birthday;
    private int carNum;
    private List<CarBean> carList;

1、根據汽車id查詢汽車,同時需要查詢該汽車的車主。

    <select id="findById" resultMap="carMap">
        select * from t_car where pk_carId = #{id};
    </select>

    <resultMap id="carMap" type="CarBean">
        <id column="pk_carId" property="id"></id>
        <result column="c_type" property="type"></result>
        <result column="c_price" property="price"></result>
        <result column="m_name" property="man.name"></result>
        <!--property為關聯屬性,select表示該屬性的值來自哪個語句塊查詢結果,column表示該語句塊傳遞哪個列的值-->
        <association property="man" select="findMan" column="fk_manId"></association>
    </resultMap>

    <select id="findMan" resultMap="manMap">
        select * from t_man where pk_manId = #{id};
    </select>

    <resultMap id="manMap" type="ManBean">
        <id property="id" column="pk_manId"></id>
        <result property="name" column="m_name"></result>
        <result property="birthday" column="m_birthday"></result>
    </resultMap>

2、根據車主id查詢車主,同時需要查詢該車主的所有汽車

①關聯語句塊

   <select id="findById" resultMap="manMap">
      select * from t_man where pk_manId = #{id};
   </select>

   <resultMap id="manMap" type="ManBean">
      <id column="pk_manId" property="id"></id>
      <result property="name" column="m_name"></result>
      <result property="birthday" column="m_birthday"></result>
      <result property="carNum" column="carNum"></result>
      <collection property="carList" select="findCar" column="pk_manId"></collection>
   </resultMap>

   <select id="findCar" resultMap="carMap">
      select * from t_car where fk_manId = #{id};
   </select>

   <resultMap id="carMap" type="CarBean">
      <id property="id" column="pk_carId"></id>
      <result property="type" column="c_type"></result>
      <result property="price" column="c_price"></result>
   </resultMap>

②連表查詢

   <resultMap id="manMap" type="ManBean">
      <id column="pk_manId" property="id"></id>
      <result property="name" column="m_name"></result>
      <result property="birthday" column="m_birthday"></result>
      <result property="carNum" column="carNum"></result>
      <!--對集合進行映射,ofType指定集合中存放元素的類型-->
      <collection property="carList" ofType="CarBean">
         <id property="id" column="pk_carId"></id>
         <result property="type" column="c_type"></result>
         <result property="price" column="c_price"></result>
      </collection>
   </resultMap>

   <select id="findById" resultMap="manMap">
      SELECT * FROM t_man m LEFT JOIN t_car c ON m.`pk_manId` = c.`fk_manId` WHERE m.`pk_manId` = #{id};
   </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM