案例表結構:
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、延遲加載==