Mybatis联合查询积累
2020-01-19 本文已影响0人
JayMeWangGL
要求如下:
从订单表中查询到订单id(ordersId),再通过查询中间表找到旅客id(travellerId),最后取得旅客信息。
EER
具体实现如下
OrdersDao
@Select("select * from orders where id=#{orderId}")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "orderNum",property = "orderNum"),
@Result(column = "orderTime",property = "orderTime"),
@Result(column = "peopleCount",property = "peopleCount"),
@Result(column = "orderDesc",property = "orderDesc"),
@Result(column = "payType",property = "payType"),
@Result(column = "orderStatus",property = "orderStatus"),
@Result(column = "productId",property = "product",javaType = Product.class,one = @One(select = "com.wgl.dao.ProductDao.findById")),
@Result(column = "touristsId",property = "tourists",javaType = Tourists.class,one = @One(select = "com.wgl.dao.TouristsDao.findById")),
@Result(column = "id",property = "travellers",javaType = java.util.List.class,many = @Many(select = "com.wgl.dao.TravellerDao.findByOrdersId")),
})
Orders findById(int orderId) throws Exception;
TravellerDao
@Select("select * from traveller where id in(select travellerId from order_traveller where orderId=#{ordersId})")
public List<Traveller> findByOrdersId(Integer ordersId);