ibatis一对多关联查询,为什么子查询没执行详细如下:
店铺(shop)与买家(buyer)为多对对关系,中间关系表(shop_buyer)现在查询买家,需要包含该买家所有关联的店铺,查询的结果集:resultMap:buyerResult
<resultMap id="buyerResult" class="buyer">
<result property="uid" column="b.uid" />
<result property="bid" column="b.bid" />
<result property="nick" column="b.nick" />
<result property="name" column="b.name" />
<result property="sex" column="b.sex" />
<result property="type" column="b.type" />
<result property="group.gid" column="b.gid" />
<result property="group.name" column="g.name" />
<result property="wangwang" column="b.wangwang" />
<result property="shops" column="b.bid" select="Buyer.selectShopByBid" />
</resultMap>
   根据买家ID通过关系表查询店铺信息的子查询:
<select id="selectShopByBid" resultClass="com.haomai.cs.model.Shop" parameterClass="int">
select s.sid,s.short_title,s.title,s.nick from `shop` s, `shop_buyer` sb where s.sid=sb.sid 
and sb.bid = #bid#
</select>执行后buyerResult结果集中其他的数据都有,只有shops属性没有数据,当setShops时总报NullPointerException错误
log4j中也没有发现执行Buyer.selectShopByBid的sql语句,为什么会这样

解决方案 »

  1.   

    sql中的字段和SqlMap中的没有对上吧。
      

  2.   


    不是很清楚,没高过复杂的ibatis查询,答案出来了告诉一下,谢谢楼主。
      

  3.   

    <result property="shops" column="b.bid" select="Buyer.selectShopByBid" /> 
    select="Buyer.selectShopByBid"  这个是不是不应该写在resultMap下面,而是写在select 下面啊? 
      

  4.   


    对上了,返回的resultMap:buyerResult中其他的值都有了,只是子查询的shops没有值,为null
      

  5.   

    <result property="shops" column="b.bid" select="Buyer.selectShopByBid" /> 
    这样的查询每次只能返回一个值,如果是多个值同时返回的话就得不到值,而且这种查询效率很低.
    建议使用三表关联来检索数据.
    select b.*,s.* from buyer as b
    join
    shop_buyer as sb
    on sb.关联字段=a.关联字段
    join
    shop as s
    on s.关联字段=sb.关联字段
    where 条件
      

  6.   

    <select id="selectShopByBid" resultClass="com.haomai.cs.model.Shop" parameterClass="int">
    select s.sid,s.short_title,s.title,s.nick from `shop` s, `shop_buyer` sb where s.sid=sb.sid  
    and sb.bid = #value# <!--#bid#-->
    </select>??
      

  7.   

    简单的关联关系在配置文件中怎么写?一对一和一对多?数据库中的字段是什么,截图显示?ibatis的