订单和订单详情的一对一 ,一对多映射
Posted xsy20181001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了订单和订单详情的一对一 ,一对多映射相关的知识,希望对你有一定的参考价值。
我们知道,订单和订单详情是一对多的关系,所以在订单Bean(OrderBean)中包含一个订单详情Bean(OrderDetailBean)的集合.
开发步骤:
1、先建好OrderDetailBean
1 package com.november.query.zuoye1; 2 3 import java.sql.Date; 4 5 public class OrderDetailBean { 6 7 //订单详情实体 8 private int orderDetailId; 9 private int orderId; 10 private int goodsId; 11 private int orderAmount; 12 private String orderMoneyPayType; 13 private Date orderPayTime; 14 private int isReview; 15 16 17 public OrderDetailBean() { 18 super(); 19 } 20 21 22 public OrderDetailBean(int orderDetailId, int orderId, int goodsId, 23 int orderAmount, String orderMoneyPayType, Date orderPayTime, 24 int isReview) { 25 super(); 26 this.orderDetailId = orderDetailId; 27 this.orderId = orderId; 28 this.goodsId = goodsId; 29 this.orderAmount = orderAmount; 30 this.orderMoneyPayType = orderMoneyPayType; 31 this.orderPayTime = orderPayTime; 32 this.isReview = isReview; 33 } 34 35 36 public int getOrderDetailId() { 37 return orderDetailId; 38 } 39 40 41 public void setOrderDetailId(int orderDetailId) { 42 this.orderDetailId = orderDetailId; 43 } 44 45 46 public int getOrderId() { 47 return orderId; 48 } 49 50 51 public void setOrderId(int orderId) { 52 this.orderId = orderId; 53 } 54 55 56 public int getGoodsId() { 57 return goodsId; 58 } 59 60 61 public void setGoodsId(int goodsId) { 62 this.goodsId = goodsId; 63 } 64 65 66 public int getOrderAmount() { 67 return orderAmount; 68 } 69 70 71 public void setOrderAmount(int orderAmount) { 72 this.orderAmount = orderAmount; 73 } 74 75 76 public String getOrderMoneyPayType() { 77 return orderMoneyPayType; 78 } 79 80 81 public void setOrderMoneyPayType(String orderMoneyPayType) { 82 this.orderMoneyPayType = orderMoneyPayType; 83 } 84 85 86 public Date getOrderPayTime() { 87 return orderPayTime; 88 } 89 90 91 public void setOrderPayTime(Date orderPayTime) { 92 this.orderPayTime = orderPayTime; 93 } 94 95 96 public int getIsReview() { 97 return isReview; 98 } 99 100 101 public void setIsReview(int isReview) { 102 this.isReview = isReview; 103 } 104 105 106 @Override 107 public String toString() { 108 return "OrderDetialBean [orderDetailId=" + orderDetailId + ", orderId=" 109 + orderId + ", goodsId=" + goodsId + ", orderAmount=" 110 + orderAmount + ", orderMoneyPayType=" + orderMoneyPayType 111 + ", orderPayTime=" + orderPayTime + ", isReview=" + isReview 112 + "]"; 113 } 114 115 116 117 }
2、建好OrderBean,里面放一个OrderDetailBean的集合
1 package com.november.query.zuoye1; 2 3 import java.sql.Date; 4 import java.util.List; 5 6 //订单实体 7 public class OrderBean { 8 9 private int orderId; 10 private int orderNo; 11 private String userName; 12 private int orderState; 13 private int addressId; 14 private Date orderTime; 15 16 //订单对应多个订单详情 一对多的关系,所以维护一个 集合对象 17 List<OrderDetailBean> orderDetail; 18 19 public OrderBean() { 20 super(); 21 } 22 23 public OrderBean(int orderId, int orderNo, String userName, int orderState, 24 int addressId, Date orderTime, List<OrderDetailBean> orderDetail) { 25 super(); 26 this.orderId = orderId; 27 this.orderNo = orderNo; 28 this.userName = userName; 29 this.orderState = orderState; 30 this.addressId = addressId; 31 this.orderTime = orderTime; 32 this.orderDetail = orderDetail; 33 } 34 35 public int getOrderId() { 36 return orderId; 37 } 38 39 public void setOrderId(int orderId) { 40 this.orderId = orderId; 41 } 42 43 public int getOrderNo() { 44 return orderNo; 45 } 46 47 public void setOrderNo(int orderNo) { 48 this.orderNo = orderNo; 49 } 50 51 public String getUserName() { 52 return userName; 53 } 54 55 public void setUserName(String userName) { 56 this.userName = userName; 57 } 58 59 public int getOrderState() { 60 return orderState; 61 } 62 63 public void setOrderState(int orderState) { 64 this.orderState = orderState; 65 } 66 67 public int getAddressId() { 68 return addressId; 69 } 70 71 public void setAddressId(int addressId) { 72 this.addressId = addressId; 73 } 74 75 public Date getOrderTime() { 76 return orderTime; 77 } 78 79 public void setOrderTime(Date orderTime) { 80 this.orderTime = orderTime; 81 } 82 83 public List<OrderDetailBean> getOrderDetail() { 84 return orderDetail; 85 } 86 87 public void setOrderDetail(List<OrderDetailBean> orderDetail) { 88 this.orderDetail = orderDetail; 89 } 90 91 @Override 92 public String toString() { 93 return "OrderBean [orderId=" + orderId + ", orderNo=" + orderNo 94 + ", userName=" + userName + ", orderState=" + orderState 95 + ", addressId=" + addressId + ", orderTime=" + orderTime 96 + ", orderDetail=" + orderDetail + "]"; 97 } 98 99 100 101 }
3、创建接口OrderMapper.java 和 OrderMapper.xml ,注意:这两个的命名必须相同
OrderMapper.java写各种方法的接口,代码如下:
1 package com.november.query.zuoye1; 2 3 import java.util.List; 4 5 public interface OrderMapper { 6 7 List<OrderBean> findOrderList(); 8 9 }
OrderMapper.xml 代码如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <!-- namespace用于区分 sql标签 ,一般为了保证唯一,namespace表示为 包名+xml文件名 --> 7 <mapper namespace="com.november.query.zuoye1.OrderMapper"> 8 9 10 <!-- select * from t_Order ord join t_OrderDetail detail on ord.OrderId = detail.OrderId --> 11 <!-- 查询所有订单:resultMap就是结果集映射 --> 12 <select id="findOrderList" resultMap="orderMap"> 13 select * from t_Order ord join t_OrderDetail detail on ord.OrderId = detail.OrderId 14 </select> 15 16 17 <!-- 自动映射 :当属性名和 字段名一致时,会自动将字段值 装配到 属性中--> 18 <!-- id属性 ,resultMap标签的标识。 19 type属性 ,返回值的全限定类名,或类型别名。 20 autoMapping属性 ,值范围true(默认值)|false, 设置是否启动自动映射功能, 21 自动映射功能就是自动查找与字段名小写同名的属性名,并调用setter方法。 22 而设置为false后,则需要在resultMap内明确注明映射关系才会调用对应的setter方法 23 --> 24 <resultMap type="OrderBean" id="orderMap" autoMapping="true"> 25 <!-- 主键 --> 26 <id property="orderId" column="OrderId"/> 27 <!-- 其他属性 --> 28 <result property="orderNo" column="OrderNo"/> 29 <result property="userName" column="UserName"/> 30 <result property="orderState" column="OrderState"/> 31 <result property="addressId" column="AddressId"/> 32 <result property="orderTime" column="OrderTime"/> 33 34 <!-- 一对多关系映射 --> 35 <!-- property="orderDetailList"的orderDetailList是OrderBean的集合属性 --> 36 <collection property="orderDetailList" ofType="OrderDetailBean" autoMapping="true"> 37 <!-- orderDetailList 实体属性 和 t_OrderDetail 表字段的映射 --> 38 39 <id property="orderDetailId" column="OrderDetailId"/> 40 <result property="orderId" column="OrderId"/> 41 <result property="goodsId" column="GoodsId"/> 42 <result property="orderAmount" column="OrderAmount"/> 43 <result property="orderMoneyPayType" column="OrderMoneyPayType"/> 44 <result property="orderPayTime" column="OrderPayTime"/> 45 <result property="isReview" column="IsReview"/> 46 47 </collection> 48 49 </resultMap> 50 51 </mapper>
4、建立一个测试类 OrderTest
1 package com.november.query.zuoye1; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 public class OrderTest { 15 16 private SqlSessionFactory sqlSessionFactory = null; 17 //在执行测试方法之前 执行的 方法 18 @Before 19 public void init(){ 20 //对mybatis环境进行 初始化 21 String resource = "config/mybatis.xml"; 22 23 try { 24 //1)读取配置文件 25 InputStream input = Resources.getResourceAsStream(resource); 26 //2)使用SQlSessionFactoryBuilder 创建SqlSessionFactory 27 sqlSessionFactory = new SqlSessionFactoryBuilder().build(input); 28 29 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 36 @Test 37 public void findOrderTest(){ 38 39 SqlSession session = sqlSessionFactory.openSession(); 40 OrderMapper mapper = session.getMapper(OrderMapper.class); 41 List<OrderBean> list = mapper.findOrderList(); 42 session.close(); 43 System.out.println(list); 44 } 45 46 }
5、运行结果
以上是关于订单和订单详情的一对一 ,一对多映射的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis一对多查询的两种姿势,你值得拥有(收藏就完事了)