mybatis多对多查询测试:订单展示效果

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 {
		//初始化sqlSessionFactory
		InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		//初始化SqlSession
		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;//1的一方
	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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值