在三表查询时,我的collection集合还能获取到一条数据,找了3小时,最终发现是因为主键名重复(我sql查询语句中第二个表没有写主键,导致我忽略了主键不能重复),还是之前犯过的错误,细微且致命!
一:原代码:
我的Mapper映射如下: 三表查询select标签如下:
<select id="selectOrder" resultMap="orderMap">
select d_order.*,orderItem.count,orderItem.amount, book.book_name,book.Dangprice,book.product_image from d_order left join order_item orderItem on d_order.id=orderItem.order_id left join d_book book on book.book_id=orderItem.product_id where d_order.user_id=1 order by d_order.ordertime desc
</select>
二、修改后的代码:
映射不变,只需给主键名字重复起别名即可,修改sql语句如下:
<select id="selectOrderById" resultMap="orderMap">
select d_order.*,order_item.id order_item_id,order_item.count count,order_item.amount,d_book.book_name,d_book.Dangprice,d_book.product_image from d_order left join order_item on d_order.id=order_item.order_id left join d_book on d_book.book_id=order_item.product_id where d_order.id=#{orderId}
</select>
三、总结
任何细小的过失都要付出沉重的代价!!
|