返回实体类中有一个集合,该集合需要映射的时候进行collection处理
public class GoodVo {
public int id;
public String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Order> orderList;
public Set<Order> getOrderList() {
return orderList;
}
public void setOrderList(Set<Order> orderList) {
this.orderList = orderList;
}
}
mapper.xml部分文件:
就算字段和数据库一致,也需要resultMap去挨个映射一遍,然后集合用collection对应。因为返回的是一个对象,所以当返回是多个对象就会出错,如Expected one result (or null) to be returned by selectOne(), but found: 2
<resultMap type="mptest.mybatistest.entity.GoodVo" id="userMap" >
<result column="id" property="id" />
<result column="name" property="name" />
<collection property="orderList" ofType="mptest.mybatistest.entity.Order">
<result column="orderId" property="orderId" />
<result column="orderFee" property="orderFee" />
</collection>
</resultMap>
<select id="getGoodsList" resultMap="userMap" >
select u.id,
u.name,
o.orderId,
o.orderFee
FROM
user_test u left join order_id_test o on u.id=o.id
where u.id = 1
</select>