mybatis 關聯表查詢


這段時間由於項目上的需求:需要將數據庫中兩表關聯的數據查詢出來展示到前端(包含一對一,一對多);

(1)一對一:

在實體類中維護了另一個類的對象:

這里我以用戶(User)和產品(Product)為例:其中get和set的方法我就沒有生成了,請自行生成;

實體類:

public class User {

   private String id;

   private String name;

   private String password;

    private Product product;

}

public class Product {

  

    private String id;

 

 

    private String name;

 

    

    private String code;

 

 

    private String userId;

 

   

    private String userName;

}

查詢方式:

聯表查詢:

sql語句:

<select id="selectAll" resultMap="allResultMap" parameterType="java.lang.String" >

    select 

    u.*,p.id p_id,p.name p_name,p.*

    from user u,product p

    where p.user_id=u.id and u.id=#{id}

  </select>

表和實體的映射關系

<resultMap id="allResultMap" type="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    <id column="id" property="id" jdbcType="VARCHAR" />

    <result column="name" property="name" jdbcType="VARCHAR" />

    <result column="password" property="password" jdbcType="VARCHAR" />

    <association property="product"  javaType="bz.sunlight.entity.Product">

             <id column="p_id" property="id" jdbcType="VARCHAR" />

             <result column="p_name" property="name" jdbcType="VARCHAR" />

         <result column="code" property="code" jdbcType="VARCHAR" />

         <result column="user_id" property="userId" jdbcType="VARCHAR" />

         <result column="user_name" property="userName" jdbcType="VARCHAR" />

    </association>

  </resultMap>

這里是將查詢出來的結果嵌套到實體對象中:

需要說明一點:這里user表和product表都用了id,name作為column名稱,所以需要在sql語句中指定別名;否則同名的字段會出現封裝到實體類上出錯;

還有一種方式是嵌套查詢:需要執行兩次查詢達到我們預期的效果,這里就不做介紹了;

(2)一對多:

需要在實體類的“一”方中維護多方的數組:

需要將User類中維護product類更改為:

  private List<Product> products;

sql:

<select id="selectAll" resultMap="allResultMap" parameterType="java.lang.String" >

    select 

    u.*,p.id p_id,p.name p_name,p.*

    from user u,product p

    where p.user_id=u.id and u.id=#{id}

  </select>

表和實體的映射關系

<resultMap id="allResultMap" type="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    <id column="id" property="id" jdbcType="VARCHAR" />

    <result column="name" property="name" jdbcType="VARCHAR" />

    <result column="password" property="password" jdbcType="VARCHAR" />

    <collection property="products"  ofType="bz.sunlight.entity.Product">

             <id column="p_id" property="id" jdbcType="VARCHAR" />

             <result column="p_name" property="name" jdbcType="VARCHAR" />

         <result column="code" property="code" jdbcType="VARCHAR" />

         <result column="user_id" property="userId" jdbcType="VARCHAR" />

         <result column="user_name" property="userName" jdbcType="VARCHAR" />

    </collection>

  </resultMap>

這里的collection標簽來映射一對多的關系,ofType:指定封裝類型;

同樣的:如果你的兩個表中的column名稱有重復,則通過起別名來處理;

到這里:聯表查詢就Ok了;

 


免責聲明!

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



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