1. 程式人生 > 程式設計 >MyBatis中的表關聯查詢實現示例

MyBatis中的表關聯查詢實現示例

Mybatis中的一對多物件關聯查詢查詢

模擬情景,商品與商品詳情:一件商品可以對應多個商品詳情資訊,即從商品➡商品詳情方向看,屬於一對多。 在一對多關係中,需要在屬於一的一方的實體類中新增多的一方的集合,一般為List<>型別

 //(省去了get和set的方法)
 public class Goods {
  private Integer goodsId ;
  private String title ;
  private String subTitle ;
  private Float originalCost ;
  private Float currentPrice ;
  private Float discount ;
  private Integer isFreeDelivery ;
  private Integer categoryId ;
  //在一對多關係中,在一方新增多的一方的集合
  private List<GoodsDetail> goodsDetailLists ; 
}

在"一方"實體類對應的xml 檔案中,新增配置資訊

<!--  OneToMany物件關聯查詢
    resultMap可用於說明一對多或者多對一的對映邏輯
    id 是resultMap屬性引用的標誌
    type 指向One的實體(Goods)
-->
  <resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods">
<!--    對映goods物件的主鍵到goods_id欄位-->
    <id column="goods_id" property="goodsId"></id>
<!--
    collection的含義是,在
    sql語句得到結果後,對所有Goods物件遍歷得到goods_id欄位值,
    並代入到goodsDetail名稱空間的selectByGoodsId的sql中執行查詢
    將得到的“商品詳情”集合賦值給goodsDetailsList物件
-->
    <collection property="goodsDetailLists" select="goodsDetail.selectByGoodsId"
          column="goods_id"></collection>
  </resultMap>
  <select id="selectOneToMany" resultMap="rmGoods1">
    select * from t_goods limit 0,1
  </select>

在“多方”對應的xml檔案中新增

<mapper namespace="goodsDetail">
  <select id="selectByGoodsId" parameterType="Integer"
      resultType="com.imooc.mybatis.entity.GoodsDetail">
    select * from t_goods_detail where goods_id = #{value}
  </select>
</mapper>

至此,關於商品到商品詳情的一對多查詢配置就完成了。

測試

 //OneToMany
  @Test
  public void selectOneToMany(){
    SqlSession sqlSession = null ;
    try{
      sqlSession = MybatisUtils.openSession() ;
      List<Goods> list = sqlSession.selectList("goods.selectOneToMany");
      for (Goods g : list){
      //輸出商品和該商品的詳情資訊數量
        System.out.println(g.getTitle() + ":" + g.getGoodsDetailLists().size());
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      MybatisUtils.closeSession(sqlSession);
    }
  }

Mybatis多對一物件關聯查詢

在上訴情景中,商品詳情➡商品即為多對一的關係
在多對一關係中,需要在多的一方的實體類中新增一的一方的實體物件

public class GoodsDetail {
  private Integer gdId ;
  private Integer goodsId ;
  private String gdPicUrl ;
  private Integer gdOrder ;
  //多對一:在多的一方新增一的一方的實體
  private Goods goods ;
}

在多的一方xml檔案中新增

<!--  多對一關係-->
  <resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
    <id column="gd_id" property="gdId"></id>
    <result column="goods_id" property="goodsId"></result>
    <!-- goods.selectById 為goods.xml根據主鍵id查詢goods資訊。-->
    <association property="goods" select="goods.selectById" column="goods_id"></association>
  </resultMap>
  <select id="selectManyToOne" resultMap="rmGoodsDetail">
    select * from t_goods_detail limit 0,1
  </select>

測試

/**
   * 多對一物件關聯對映
   * */
  @Test
  public void selectManyToOne(){
    SqlSession sqlSession = null ;
    try{
      sqlSession = MybatisUtils.openSession() ;
      List<GoodsDetail> list = sqlSession.selectList("goodsDetail.selectManyToOne");
      for (GoodsDetail gd : list){
        System.out.println(gd.getGdPicUrl() + ":" + gd.getGoods().getTitle());
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      MybatisUtils.closeSession(sqlSession);
    }
  }

到此這篇關於MyBatis中的表關聯查詢實現示例的文章就介紹到這了,更多相關MyBatis 表關聯查詢內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!