<resultMap type="com.mybatis.bean.Customer" id="BaseResultMap">
<id column="cust_id" property="custId"/>
<result column="cust_name" property="custName"/>
<result column="cust_age" property="custAge"/>
<collection property="orderSet" ofType="com.mybatis.bean.Order" column="cust_id"
select="com.mybatis.mapper.OrderMapper.getOrderByCustId">
</collection>
</resultMap>
<select id="getCustomerByCustId" resultMap="BaseResultMap">
select cust_id,cust_name,cust_age
from tbl_cust
where cust_id=#{custId}
</select>
<select id="getCustomerAndOrederByCustId" resultMap="BaseResultMap">
select cust_id,cust_name,cust_age
from tbl_cust
where cust_id=#{custId}
</select>
在执行getCustomerByCustId方法时不需要关联查询,不执行resultMap中的collection,在执行getCustomerAndOrederByCustId方法是需要执行关联查询,我知道在getCustomerByCustId方法的查询语句中不查询cust_id可以解决这个问题,但是现在的需求是我要放回的结果中包含cust_id的,不知道这个该怎么处理?求大神指点迷津。。。
建议你单独定义两个resultMap,其中一个有collection映射,一个没有colleaction映射,查询的时候分别映射不同的resultMap就行了。
分开定义查询返回值是多少,再看一下它传过的我映射的结果,
这种不能放到list里面,但是他支持数组,所以list.toArray()就可以解决
"我知道在getCustomerByCustId方法的查询语句中不查询cust_id可以解决这个问题",
查不查询一是样的,同样的列可以在一个查询中批定多次,你这里声明两次就行了,一个就查询列值映射到cust_id,另一个一对多映射到orders.
如果使用注释更方便:
public class Customer extends BaseEntity{
private Integer cust_id;//报表代码
private String cust_name;//数据表名称
private Integer cust_age; //序号
private List orders;//分类实体
public Customer() {
super();
}
public Integer getCust_id() {
return cust_id;
}
public void setCust_id(Integer cust_id) {
this.cust_id = cust_id;
}
@Select("select * from Order where cust_id=#{cust_id}")
public List<Order> getOrders(int id);
}