CustomerMapper.xml映射配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abc.mybatis.mapper.CustomerMapper">
<!--
public List<Customer> queryAllCustomer(); -->
<select id="queryAllCustomer" resultMap="customerAndOrderAndOrderDetailAndProductMap">
<!-- 写sql语句就能看到结果集,一定要测试下 -->
select c.cid,c.cname,c.c_phone,c.c_address,
o.order_id,o.order_date,
od.order_detail_id,od.p_number,od.p_price,
p.p_id,p.p_name
from customer c,orderes o,order_detail od,product p
where c.cid=o.cid and od.order_id=o.order_id and od.p_id=p.p_id
</select>
<resultMap type="Customer" id="customerAndOrderAndOrderDetailAndProductMap">
<!-- 没法写,因为没有看到结果集 -->
<!-- 先对Customer类型的属性进行映射 -->
<!-- 接下来对Order类型进行映射,所有需要在Customer类中定义集合属性List<Order> -->
<!-- 然后再对订单详情进行映射,所以订单详情需要是订单类的一个集合属性 -->
<!-- 最后对商品进行映射,所以商品是订单详情类的一个属性 -->
<id column="cid" property="cId" />
<result column="cname" property="cName" />
<result column="c_phone" property="cPhone" />
<result column="c_address" property="cAddress" />
<collection property="orders" ofType="Order" >
<id column="order_id" property="orderId" />
<result column="order_date" property="orderDate" />
<collection property="orderDetails" ofType="OrderDetail" >
<id column="order_detail_id" property="orderDetailId" />
<result column="p_number" property="pNumber" />
<result column="p_price" property="pPrice" />
<association property="product" javaType="Product">
<id column="p_id" property="pId" />
<result column="p_name" property="pName" />
</association>
</collection>
</collection>
</resultMap>
</mapper>
测试类
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.abc.mybatis.domain.Customer;
import com.abc.mybatis.domain.Order;
import com.abc.mybatis.domain.OrderDetail;
import com.abc.mybatis.mapper.CustomerMapper;
public class TestMany2ManyMapper {
public static void main(String[] args) throws IOException {
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession=sessionFactory.openSession();
CustomerMapper mapper=sqlSession.getMapper(com.abc.mybatis.mapper.CustomerMapper.class);
List<Customer> cs=mapper.queryAllCustomer();
Iterator<Customer> it=cs.iterator();
while(it.hasNext())
{
Customer c=it.next();
System.out.println("客户信息:"+c.getcId()+","+c.getcName()+","+c.getcPhone()+","+c.getcAddress());
List<Order> orders=c.getOrders();
Iterator<Order> it_order=orders.iterator();
while(it_order.hasNext())
{
Order o=it_order.next();
System.out.println("\t订单信息:"+o.getOrderId()+","+o.getOrderDate());
List<OrderDetail> orderDetails=o.getOrderDetails();
Iterator<OrderDetail> it_orderdetail=orderDetails.iterator();
while(it_orderdetail.hasNext())
{
OrderDetail od=it_orderdetail.next();
System.out.println("\t\t订单详情信息:"+od.getOrderDetailId()+","+od.getProduct().getpName()+","+
od.getpPrice()+","+od.getpNumber());
}
}
}
}
}
CustomerMapper.java接口
package com.abc.mybatis.mapper;
import java.util.List;
import com.abc.mybatis.domain.Customer;
public interface CustomerMapper {
public List<Customer> queryAllCustomer();
}
Customer.java用户实体类
package com.abc.mybatis.domain;
import java.util.List;
public class Customer {
private Integer cId;
private String cName;
private String cPhone;
private String cAddress;
public List<Order> orders;
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public Integer getcId() {
return cId;
}
public void setcId(Integer cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getcPhone() {
return cPhone;
}
public void setcPhone(String cPhone) {
this.cPhone = cPhone;
}
public String getcAddress() {
return cAddress;
}
public void setcAddress(String cAddress) {
this.cAddress = cAddress;
}
@Override
public String toString() {
return "Customer [cId=" + cId + ", cName=" + cName + ", cPhone=" + cPhone + ", cAddress=" + cAddress + "]\n";
}
}
Order.java订单实体类
package com.abc.mybatis.domain;
import java.util.Date;
import java.util.List;
public class Order {
private Integer orderId;
private Customer customer;
private Date orderDate;
private List<OrderDetail> orderDetails;
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public List<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(List<OrderDetail> orderDetail) {
this.orderDetails = orderDetail;
}
@Override
public String toString() {
return "Order [orderId=" + orderId + ", customer=" + customer + ", orderDate=" + orderDate + "]\n";
}
}
OrderDetail.java订单明细实体类
package com.abc.mybatis.domain;
public class OrderDetail {
private Integer orderDetailId;
private Product product;
private Integer pNumber;
private Double pPrice;
private Order order;
public Integer getOrderDetailId() {
return orderDetailId;
}
public void setOrderDetailId(Integer orderDetailId) {
this.orderDetailId = orderDetailId;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getpNumber() {
return pNumber;
}
public void setpNumber(Integer pNumber) {
this.pNumber = pNumber;
}
public Double getpPrice() {
return pPrice;
}
public void setpPrice(Double pPrice) {
this.pPrice = pPrice;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString() {
return "OrderDetail [orderDetailId=" + orderDetailId + ", pId=" + product.getpId() +", pNumber=" + pNumber + ", pPrice=" + pPrice + "]";
}
}
Product.java商品实体类
package com.abc.mybatis.domain;
public class Product {
private Integer pId;
private String pName;
public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
}