欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ibatis一对多的延迟加载应用

程序员文章站 2022-05-23 13:01:36
...

我们的应用之中有一个一对多的情况 :

 

一个批发产品对应3条批发价格信息

 

于是model 结构 ; 

 

public class ProductVo implements Serializable {


    private List<WholesaleRange> wholeRageList;       //价格信息集合

    public List<WholesaleRange> getWholeRageList() {
        return wholeRageList;
    }

    public void setWholeRageList(List<WholesaleRange> wholeRageList) {
        this.wholeRageList = wholeRageList;
    }


}

 

如何查询批发信息的时候直接带出附加的价格信息集合呢 ?

 

SQLMAP 如下构建:

 

<typeAlias alias="wholesaleprd" type="com.woyo.business.wholesale.domain.model.ProductVo" />

  <resultMap id="wholesalePriceRageMap" class="wholesaleprd" >
    <result column="id" property="id" jdbcType="VARCHAR" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="is_mixed_batch" property="mixedBatch" nullValue="0" />
    <result column="id" property="wholeRageList" select="getWholeRageList"/>
  </resultMap>



<select id="getWholeRageList" resultClass="wholesaleRage" parameterClass="long">
    select id as id , whilesale_id as whilesaleId , quantity as quantity , price as price from wholesale_range where whilesale_id = #value# order by quantity
</select>



 <select id="getWholeSaleById" resultMap="wholesalePriceRageMap" parameterClass="long">
    SELECT w.id as id ,w.title as title, p.id as prdId,p.product_name AS prdName ,p.count - w.order_amount AS balance_amount ,
    p.price AS price, p.image_url AS imageUrl, w.supplier_name, w.supplier_tel, w.wholesale_amount,
    w.wholesale_price, w.team_price, w.team_save, w.order_amount, w.team_amount ,w.is_mixed_batch
    FROM wholesale w left join  product  p on w.product_id = p.id
    
    where w.id = #value#
 </select>

 

 

这样, 查询出的对象, Ibatis 会用延迟加载的策略来得到 wholeRageList 

 

 

相关标签: iBATIS