mybatis一对多

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;


User类(com.changez.sm.base.bean.User)主要属性:
	private String id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;


ordertail类(com.changez.sm.base.bean.Ordertail)主要属性:

	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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值