MyBatis多对多关联

<!-- 多对多映射 -->
	<resultMap type="com.leech.mybatis.first.pojo.User" id="UserAndItemsResultMap">
		<!-- 用户信息 -->
		<id property="id" column="user_id"/>
		<result property="username" column="username"/>
		<result property="sex" column="sex"/>
		<result property="address" column="address"/>
		
		<!-- 订单信息 -->
		<collection property="ordersList" ofType="com.leech.mybatis.first.pojo.Orders">
			<id property="id" column="id"/>
			<result property="userId" column="user_id"/>
			<result property="number" column="number"/>
			<result property="createtime" column="createtime"/>
			<result property="note" column="note"/>
			
			<!-- 订单详情 -->
			<collection property="orderdetails" ofType="com.leech.mybatis.first.pojo.Orderdetail">
				<id column="orderdetail_id" property="id"/>
				<result property="ordersId" column="orders_id"/>
				<result property="itemsId" column="items_id"/>
				<result property="itemsNum" column="items_num"/>
				
				<!-- 商户信息 -->
				<association property="items" javaType="com.leech.mybatis.first.pojo.Items">
					<id property="id" column="items_id"/>
					<result property="name" column="items_name"/>
					<result property="price" column="items_price"/>
					<result property="pic" column="items_pic"/>
					<result property="createtime" column="items_createtime"/>
					<result property="detail" column="items_detail"/>
				</association>
			</collection>
		</collection>
		
	</resultMap>
	
	<!-- 查询用户购买的商品信息 -->
	<select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
		select orders.*,
				user.id user_id,user.username,user.sex,user.address,
				orderdetail.id orderdetail_id,orderdetail.items_id,orderdetail.items_num,orderdetail.orders_id,
				items.id items_id,items.name items_name,items.detail items_detail,items.price items_price,items.pic items_pic,items.createtime items_createtime
	 	from orders,user,orderdetail,items
		where orders.user_id=user.id and orderdetail.orders_id=orders.id and items.id=orderdetail.items_id
	</select>


你可能感兴趣的:(MyBatis多对多关联)