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语句,为什么会这样
店铺(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语句,为什么会这样
解决方案 »
- svnclientadapter 的commit怎么实现
- 对插入数据库的值进行加密
- Extjs 4.0 + Struts2 用户登陆验证问题
- 关于一个缓存操作的例子!!!
- 如何从servlet当中跳转到jsp页面,并把数据传送过去。
- 在eclipse资源管理器的文件上有问号,提示错误如下。请指点下啊~!!
- 这几天文件上传(smartupload)的问题问的特别多,建议版主和高手们将问题汇集起来解决一下,有志高手也可以在此讨论,高分相送,谢谢!
- 解决就给分,如何将tomcat5.0的端口由8080改为80?
- 紧急请教JAVA高手(在Servlet中读取文件)
- 帮忙,看看是怎么一回事?为何名字可以提交,内容就不可以
- 新手求助..
- 如何读取缓存?
不是很清楚,没高过复杂的ibatis查询,答案出来了告诉一下,谢谢楼主。
select="Buyer.selectShopByBid" 这个是不是不应该写在resultMap下面,而是写在select 下面啊?
对上了,返回的resultMap:buyerResult中其他的值都有了,只是子查询的shops没有值,为null
这样的查询每次只能返回一个值,如果是多个值同时返回的话就得不到值,而且这种查询效率很低.
建议使用三表关联来检索数据.
select b.*,s.* from buyer as b
join
shop_buyer as sb
on sb.关联字段=a.关联字段
join
shop as s
on s.关联字段=sb.关联字段
where 条件
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>??