需求:订单和商品表
(分析:一个订单针对多个商品表,一个商品也可以针对多个订单,其中订单里面有多个订单详情,一个订单详情对应一个商品!简单的来说一个订单详情和商品是一对一的关系!多对多分解成了一对多和一对多)

  1. SQL语句:
select
		u.id uid,u.user_name,u.password,u.name,u.age,u.sex,u.birthday,u.created,u.updated,o.id
		oid,o.user_id,o.order_number,od.id odid,od.order_id,od.item_id,od.total_price,od.status,
		i.id iid,i.item_name,i.item_price,i.item_detail
		from tb_user u,tb_order o,tb_orderdetail od,tb_item i
		where u.id = o.user_id and o.id = od.order_id and i.id = od.item_id and o.order_number =
		#{orderNumber}
  1. 实体类:(订单 一对多 订单详情 一对一 商品)

订单的实体类:

package com.hisoft.pojo;

import java.util.List;

public class Order {
	int id;
	long userId;
	String orderNumber;
	User user;
	List<OrderDetail> olist;
	
	public List<OrderDetail> getOlist() {
		return olist;
	}
	public void setOlist(List<OrderDetail> olist) {
		this.olist = olist;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public long getUserId() {
		return userId;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Order(int id, long userId, String orderNumber, User user) {
		super();
		this.id = id;
		this.userId = userId;
		this.orderNumber = orderNumber;
		this.user = user;
	}
	public Order() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Order [id=" + id + ", userId=" + userId + ", orderNumber=" + orderNumber + ", user=" + user + ", olist="
				+ olist + "]";
	}
}

订单详情实体类:(实际上和商品是一对一的管理,所有需要在里面创建一个商品的对象实现一对一)

package com.hisoft.pojo;

public class OrderDetail {
	int id;
	int orderId;
	int itemId;
	double totalPrice;
	int status;
	Item item;
	
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getOrderId() {
		return orderId;
	}
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}
	public int getItemId() {
		return itemId;
	}
	public void setItemId(int itemId) {
		this.itemId = itemId;
	}
	public double getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public OrderDetail(int id, int orderId, int itemId, double totalPrice, int status) {
		super();
		this.id = id;
		this.orderId = orderId;
		this.itemId = itemId;
		this.totalPrice = totalPrice;
		this.status = status;
	}
	public OrderDetail() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "OrderDetail [id=" + id + ", orderId=" + orderId + ", itemId=" + itemId + ", totalPrice=" + totalPrice
				+ ", status=" + status + ", item=" + item + "]";
	}
	
	
}

商品的实体类

package com.hisoft.pojo;

public class Item {
	int id;
	String itemName;
	float itemPrice;
	String itemDetail;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getItemName() {
		return itemName;
	}
	public void setItemName(String itemName) {
		this.itemName = itemName;
	}
	public float getItemPrice() {
		return itemPrice;
	}
	public void setItemPrice(float itemPrice) {
		this.itemPrice = itemPrice;
	}
	public String getItemDetail() {
		return itemDetail;
	}
	public void setItemDetail(String itemDetail) {
		this.itemDetail = itemDetail;
	}
	public Item(int id, String itemName, float itemPrice, String itemDetail) {
		super();
		this.id = id;
		this.itemName = itemName;
		this.itemPrice = itemPrice;
		this.itemDetail = itemDetail;
	}
	public Item() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Item [id=" + id + ", itemName=" + itemName + ", itemPrice=" + itemPrice + ", itemDetail=" + itemDetail
				+ "]";
	}
	
}
  1. 创建接口
public interface OrderMapper {
	Order getOrderUserDetailItemInfo(@Param("orderNumber")String ordernumber);
}
  1. 在OrderMapper.xml里面书写sql语句
//这里注意一点这个resultMap里面继承了一对一的resultMap(哪里不太懂了 查看下一对多,里面有对里面的属性进行解释)
	<resultMap type="Order" id="OrderUserDetailItemInfo"
		autoMapping="true" extends="OrderUserInfo">
		<collection property="olist" javaType="List"
			ofType="OrderDetail">
			<id column="odid" property="id" />
			<association property="item" javaType="Item">
				<id column="iid" property="id" />
			</association>
		</collection>
	</resultMap>

<select id="getOrderUserDetailItemInfo"
		resultMap="OrderUserDetailItemInfo">
		select
		u.id uid,u.user_name,u.password,u.name,u.age,u.sex,u.birthday,u.created,u.updated,o.id
		oid,o.user_id,o.order_number,od.id odid,od.order_id,od.item_id,od.total_price,od.status,
		i.id iid,i.item_name,i.item_price,i.item_detail
		from tb_user u,tb_order o,tb_orderdetail od,tb_item i
		where u.id = o.user_id and o.id = od.order_id and i.id = od.item_id and o.order_number =
		#{orderNumber}
	</select>