案例表结构:
1、用户表
2、订单表
3、订单明细表
4、商品表
表结构关系相对比较清楚。
0、项目代码结构
1、表结构创建
客户表
-- ---------------------------- -- Table structure for ex_customer -- ---------------------------- DROP TABLE IF EXISTS `ex_customer`; CREATE TABLE `ex_customer` ( `custId` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `custName` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `telephone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `sex` char(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`custId`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ex_customer -- ---------------------------- INSERT INTO `ex_customer` VALUES ('C001', '顾问', '15345187579', '男', '顾问'); INSERT INTO `ex_customer` VALUES ('C002', '曹阳', '15345187577', '男', '曹阳'); SET FOREIGN_KEY_CHECKS = 1;
订单表
-- ---------------------------- -- Table structure for ex_order -- ---------------------------- DROP TABLE IF EXISTS `ex_order`; CREATE TABLE `ex_order` ( `orderId` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `custId` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `orderDate` datetime NULL DEFAULT NULL, `totalMoney` decimal(10, 2) NULL DEFAULT NULL, `remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`orderId`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ex_order -- ---------------------------- INSERT INTO `ex_order` VALUES ('T0001', 'C001', '2021-01-01 00:00:00', 7000.00, '测试订单'); SET FOREIGN_KEY_CHECKS = 1;
订单明细表
-- ---------------------------- -- Table structure for ex_orderdetail -- ---------------------------- DROP TABLE IF EXISTS `ex_orderdetail`; CREATE TABLE `ex_orderdetail` ( `detailId` bigint(20) NOT NULL AUTO_INCREMENT, `orderId` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `prodId` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `buyPrice` decimal(10, 2) NULL DEFAULT NULL, `buyCount` int(11) NULL DEFAULT NULL, `remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`detailId`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ex_orderdetail -- ---------------------------- INSERT INTO `ex_orderdetail` VALUES (1, 'T0001', 'P001', 2000.00, 2, NULL); INSERT INTO `ex_orderdetail` VALUES (2, 'T0001', 'P002', 3000.00, 1, NULL); SET FOREIGN_KEY_CHECKS = 1;
商品表
-- ---------------------------- -- Table structure for ex_product -- ---------------------------- DROP TABLE IF EXISTS `ex_product`; CREATE TABLE `ex_product` ( `prodId` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `prodName` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `sellPrice` decimal(10, 2) NULL DEFAULT NULL, `imgUrl` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`prodId`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of ex_product -- ---------------------------- INSERT INTO `ex_product` VALUES ('P001', '手机', 2000.00, NULL, NULL); INSERT INTO `ex_product` VALUES ('P002', '平板', 3000.00, NULL, NULL); SET FOREIGN_KEY_CHECKS = 1;
2、模型类定义
2.1、用户模型
package db.Model; //客户模型 public class ex_Customer { private String custId; private String custName; private String telephone; private String sex; private String remark; public String getCustId() { return custId; } public void setCustId(String custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "ex_customer{" + "custId='" + custId + '\'' + ", custName='" + custName + '\'' + ", telephone='" + telephone + '\'' + ", sex='" + sex + '\'' + ", remark='" + remark + '\'' + '}'; } }
2.2、订单模型
package db.Model; import java.util.Date; import java.util.List; //订单模型 public class ex_Order { private String orderId; private String custId; private Date orderDate; private double totalMoney; private String remark; //包含客户表引用 private ex_Customer orderCust; //包含订单明细 private List<ex_OrderDetail> orderDetail; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getCustId() { return custId; } public void setCustId(String custId) { this.custId = custId; } public Date getOrderDate() { return orderDate; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } public double getTotalMoney() { return totalMoney; } public void setTotalMoney(double totalMoney) { this.totalMoney = totalMoney; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public ex_Customer getOrderCust() { return orderCust; } public void setOrderCust(ex_Customer orderCust) { this.orderCust = orderCust; } public List<ex_OrderDetail> getOrderDetail() { return orderDetail; } public void setOrderDetail(List<ex_OrderDetail> orderDetail) { this.orderDetail = orderDetail; } @Override public String toString() { String result = "ex_order{" + "orderId='" + orderId + '\'' + ", cusId='" + custId + '\'' + ", orderDate=" + orderDate + ", totalMoney=" + totalMoney + ", remark='" + remark + '\'' + '}'; if(orderCust!= null) result += "\n" + orderCust.toString(); if(orderDetail != null) { result += "\n订单包含如下的订单明细数据"; for(ex_OrderDetail detail:orderDetail) { result += "\n" + detail.toString(); } } return result; } }
2.3、订单明细模型
package db.Model; //订单明细 public class ex_OrderDetail { private long detailId; private String orderId; private String prodId; private double buyPrice; private int buyCount; private String remark; //包含订单商品引用 private ex_Product orderProd; public long getDetailId() { return detailId; } public void setDetailId(long detailId) { this.detailId = detailId; } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getProdId() { return prodId; } public void setProdId(String prodId) { this.prodId = prodId; } public double getBuyPrice() { return buyPrice; } public void setBuyPrice(double buyPrice) { this.buyPrice = buyPrice; } public int getBuyCount() { return buyCount; } public void setBuyCount(int buyCount) { this.buyCount = buyCount; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public ex_Product getOrderProd() { return orderProd; } public void setOrderProd(ex_Product orderProd) { this.orderProd = orderProd; } @Override public String toString() { String result = "ex_Orderdetail{" + "detailId=" + detailId + ", orderId='" + orderId + '\'' + ", prodId='" + prodId + '\'' + ", buyPrice=" + buyPrice + ", buyCount=" + buyCount + ", remark='" + remark + '\'' + '}'; if(orderProd != null) { result += "\n" + orderProd.toString(); } return result; } }
2.4、商品模型
package db.Model; //产品模型 public class ex_Product { private String prodId; private String prodName; private double sellPrice; private String imgUrl; private String remark; public String getProdId() { return prodId; } public void setProdId(String prodId) { this.prodId = prodId; } public String getProdName() { return prodName; } public void setProdName(String prodName) { this.prodName = prodName; } public double getSellPrice() { return sellPrice; } public void setSellPrice(double sellPrice) { this.sellPrice = sellPrice; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "ex_product{" + "prodId='" + prodId + '\'' + ", prodName='" + prodName + '\'' + ", sellPrice=" + sellPrice + ", imgUrl='" + imgUrl + '\'' + ", remark='" + remark + '\'' + '}'; } }
3、访问层接口
订单访问接口,通过订单查询其它相关的数据
package db.Dao; public interface ex_OrderDao { public db.Model.ex_Order findByKey(String orderId); }
4、映射文件定义
src/main/resources/db/mapper/ex_Order.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="db.Dao.ex_OrderDao"> <select id="findByKey" resultMap="orderResult"> Select ex_order.*, custName,telephone,sex,ex_customer.remark as ex_customer_remark, detailId,ex_orderdetail.prodId,buyCount,buyPrice, prodName,sellPrice,imgUrl from ex_order left join ex_customer on ex_order.custId = ex_customer.custId left join ex_orderdetail on ex_order.orderId = ex_orderdetail.orderId left join ex_product on ex_orderdetail.prodId = ex_product.prodId where ex_order.orderId=#{orderId} </select> <!-- 定义订单查询结果映射 --> <resultMap id="orderResult" type="db.Model.ex_Order"> <id property="orderId" column="orderId"/> <result property="custId" column="custId"/> <result property="orderDate" column="orderDate"/> <result property="totalMoney" column="totalMoney"/> <result property="remark" column="remark"/> <!--客户关联,通过javaType设置映射的类型--> <association property="orderCust" javaType="db.Model.ex_Customer" resultMap="customerResult"> </association> <!--订单明细集合,通过ofType设置映射的类型--> <collection property="orderDetail" ofType="db.Model.ex_OrderDetail" resultMap="orderDetailResult" > </collection> </resultMap> <!--客户映射--> <resultMap id="customerResult" type="db.Model.ex_Customer"> <id property="custId" column="custId"/> <result property="custName" column="custName"/> <result property="telephone" column="telephone"/> <result property="sex" column="sex"/> <!--列明重复时解决方案--> <result property="remark" column="ex_customer_remark"/> </resultMap> <!--订单明细映射--> <resultMap id="orderDetailResult" type="db.Model.ex_OrderDetail"> <id property="detailId" column="detailId"/> <result property="orderId" column="orderId"/> <result property="prodId" column="prodId"/> <result property="buyPrice" column="buyPrice"/> <result property="buyCount" column="buyCount"/> <result property="remark" column="remark"/> <!--商品关联--> <association property="orderProd" javaType="db.Model.ex_Product" resultMap="productResult"> </association> </resultMap> <!--商品映射--> <resultMap id="productResult" type="db.Model.ex_Product"> <id property="prodId" column="prodId"></id> <result property="prodName" column="prodName"/> <result property="sellPrice" column="sellPrice"/> <result property="imgUrl" column="imgUrl"/> </resultMap> </mapper>
5、测试查询
import db.Dao.ex_OrderDao; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; public class myTest { //1对1操作 @Test public void TestMyBaisc_01() throws IOException { //1 读取配置文件 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //2 构建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //3 构建SqlSession,并执行mapper内操作 try (SqlSession session = sqlSessionFactory.openSession()) { //获取接口代理实现 ex_OrderDao dao = session.getMapper(ex_OrderDao.class); db.Model.ex_Order entry = dao.findByKey("T0001"); System.out.println(entry.toString()); } System.out.println("测试完成1"); } }
运行结果:
6、多对多映射==
7、延迟加载==