orders类(com.changez.sm.base.bean.Orders)主要属性:
private String id;
private String userId;
private String orderNum;
private Date createTime;
private String note;
private User user;
private Set<Ordertail> ordertails;
private String id;
private String username;
private Date birthday;
private String sex;
private String address;
private int id;
private int orderId;
private int itemId;
private int itemNum;
一对多查询:
collection:
property:bean中集合的属性名,
ofType:集合类型全限定名
select语句:其中的resultMap有以下两种选择: queryOrderAndDetailResultMapExtends或者queryOrderAndDetailResultMap
<!-- 一对多查询 :查询订单及订单明细的信息 -->
<select id="queryOrderAndDetail" resultMap="queryOrderAndDetailResultMap">
SELECT
tco.id ordersid, tco.user_id, tco.order_num, tco.create_time,
tcd.id ordertailid, tcd.order_id, tcd.item_id, tcd.item_num,
tcu.id userid, tcu.username, tcu.birthday, tcu.sex, tcu.address
from
temp_changez_orders tco, temp_changez_ordertail tcd, temp_changez_user tcu
WHERE
tco.id=tcd.order_id AND tco.user_id=tcu.id
</select>
resultMap语句(方式一,将一对一, 一对多关系都写完整):
<resultMap type="com.changez.sm.base.bean.Orders" id="queryOrderAndDetailResultMap">
<id column="ordersid" property="id"/>
<result column="user_id" property="userId"/>
<result column="order_num" property="orderNum"/>
<result column="create_time" property="createTime"/>
<result column="note" property="note"/>
<!-- 一对一的关系配置, 此处可以删掉,不影响一对多的配置 -->
<association property="user" javaType="com.changez.sm.base.bean.User">
<id column="userid" property="id"/>
<result column="username" property="username"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
</association>
<!-- 一对多的关系配置 -->
<collection property="ordertails" ofType="com.changez.sm.base.bean.Ordertail">
<id column="ordertailid" property="id"/>
<result column="order_id" property="orderId"/>
<result column="item_id" property="itemId"/>
<result column="item_num" property="itemNum"/>
</collection>
</resultMap>
resultMap语句(方式二,在一对一的关系中已经配置了orders和user的一对一关系, 可以使用extends关键字引用已有的对照关系):
<!-- 使用extends -->
<resultMap type="com.changez.sm.base.bean.Orders" id="queryOrderAndDetailResultMapExtends" extends="queryOrderAndUserResultMap">
<collection property="ordertails" ofType="com.changez.sm.base.bean.Ordertail">
<id column="ordertailid" property="id"/>
<result column="order_id" property="orderId"/>
<result column="item_id" property="itemId"/>
<result column="item_num" property="itemNum"/>
</collection>
</resultMap>
orders和user的一对一关系配置:
<resultMap type="com.changez.sm.base.bean.Orders" id="queryOrderAndUserResultMap">
<id column="ordersid" property="id"/>
<result column="user_id" property="userId"/>
<result column="order_num" property="orderNum"/>
<result column="create_time" property="createTime"/>
<result column="note" property="note"/>
<association property="user" javaType="com.changez.sm.base.bean.User">
<id column="userid" property="id"/>
<result column="username" property="username"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
</association>
</resultMap>