多对多(转换为2个一对多)
例如:订单和产品的关系
~~~
public class Sorder {
private int oid;
private Timestamp odate;
private String ostatus;
private String oaddress;
private double totalprice;
private Userinfo u;
private List<Orderdetails> details;
}
public class Product {
private int pid;
private String pname;
private String pdesc;
private double price;
private List<Orderdetails> details;
}
public class Orderdetails {
private int id;
private double price;
private int pcount;
private double ptotalprice;
private Sorder order;
private Product p;
}
~~~
查询订单,包含用户信息,以及订单下的产品信息
映射关系
~~~
<select id="getOrderById" parameterType="int" resultMap="_orderdetails">
select s.*, od.id, od.price, od.pcount, p.pid, p.pname, p.pdesc, u.id as uid, u.name
from sorder s, orderdetails od, product p,userinfo u
where s.oid = od.oid
and od.pid = p.pid
and s.userid = u.id
and s.oid = 1
</select>
<resultMap type="Sorder" id="_orderdetails">
<id column="oid" property="oid"/>
<result column="odate" property="odate"/>
<result column="ostatus" property="ostatus"/>
<result column="totalprice" property="totalprice"/>
<result column="oaddress" property="oaddress"/>
<association property="u" javaType="Userinfo">
<id column="uid" property="id"></id>
<result column="name" property="name"/>
</association>
<collection property="details" ofType="Orderdetails" column="oid">
<id column="id" property="id"/>
<result column="price" property="price"/>
<result column="pcount" property="pcount"/>
<association property="p" javaType="Product">
<id column="pid" property="pid"></id>
<result column="pname" property="pname"/>
<result column="pdesc" property="pdesc"/>
</association>
</collection>
</resultMap>
~~~
练习:查询一个商品,以及商品包含在哪些订单下