2個實體:訂單、商品,一個訂單可以包含多種商品,同時一種商品可以屬於多個訂單,即多對多。 商品表goods_tb: 訂單表order_tb: no是訂單編號,user_id與用戶表的id關聯。 需要新建一張中間表order_item_tb,引入2個“多”的主鍵作為外鍵,把這2個“多”聯繫起來: pu ...
2個實體:訂單、商品,一個訂單可以包含多種商品,同時一種商品可以屬於多個訂單,即多對多。
商品表goods_tb:
訂單表order_tb:
no是訂單編號,user_id與用戶表的id關聯。
需要新建一張中間表order_item_tb,引入2個“多”的主鍵作為外鍵,把這2個“多”聯繫起來:
purchase_amount是該種商品的購買數量。
使用嵌套結果實現一對多
(一)編寫pojo類
package com.chy.pojo; public class Goods { private Integer id; //商品id private String goodsName; //商品名稱 private float goodsPrice; //商品單價 private Integer purchaseAmount; //購買數量 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public float getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(float goodsPrice) { this.goodsPrice = goodsPrice; } public Integer getPurchaseAmount() { return purchaseAmount; } public void setPurchaseAmount(Integer purchaseAmount) { this.purchaseAmount = purchaseAmount; } @Override public String toString() { return "Goods{" + "id=" + id + ", goodsName='" + goodsName + '\'' + ", goodsPrice=" + goodsPrice + ", purchaseAmount=" + purchaseAmount + '}'; } }
package com.chy.pojo; import java.util.List; public class Order { private Integer no; private Integer userId; private List<Goods> goodsList; //包含的商品 public Integer getNo() { return no; } public void setNo(Integer no) { this.no = no; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public List<Goods> getGoodsList() { return goodsList; } public void setGoodsList(List<Goods> goodsList) { this.goodsList = goodsList; } @Override public String toString() { return "Order{" + "no=" + no + ", userId=" + userId + ", goodsList=" + goodsList + '}'; } }
在哪個pojo中使用List來關聯其他實體,需要根據業務需求來確定。
(二)編寫Mapper介面、映射文件
package com.chy.mapper; import com.chy.pojo.Order; public interface OrderMapper { //根據orderId查詢訂單信息 public Order queryOrderByOrderNo(Integer orderNo); }
<?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.chy.mapper.OrderMapper"> <select id="queryOrderByOrderNo" parameterType="integer" resultMap="orderResultWithGoods"> SELECT order_tb.*,goods_tb.*,order_item_tb.purchase_amount FROM order_tb,goods_tb,order_item_tb WHERE order_tb.no=#{no} AND order_item_tb.order_no=order_tb.no AND goods_tb.id=order_item_tb.goods_id </select> <resultMap id="orderResultWithGoods" type="order"> <id property="no" column="no"/> <result property="userId" column="user_id"/> <collection property="goodsList" ofType="goods"> <id property="id" column="id"/> <result property="goodsName" column="goods_name"/> <result property="goodsPrice" column="goods_price"/> <result property="purchaseAmount" column="purchase_amount"/> </collection> </resultMap> </mapper>
三表聯合查詢,sql語句很長,如果覺得表名、欄位名不好寫,可以用as設置表名、欄位名的別名。
不是每個pojo類都要寫對應的Mapper介面、映射文件,寫哪些,看業務需求。
(三)使用
package com.chy.utils; 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 java.io.IOException; import java.io.InputStream; public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
package com.chy.test; import com.chy.mapper.OrderMapper; import com.chy.pojo.Order; import com.chy.utils.MyBatisUtils; import org.apache.ibatis.session.*; public class Test { public static void main(String[] args) { SqlSession sqlSession = MyBatisUtils.getSqlSession(); OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); Order order = mapper.queryOrderByOrderNo(1); System.out.println(order); sqlSession.close(); } }
結果:
Order{no=1, userId=1, goodsList=[Goods{id=1, goodsName='抽紙', goodsPrice=6.5, purchaseAmount=1}, Goods{id=2, goodsName='中華', goodsPrice=80.0, purchaseAmount=2}]}
也可以使用嵌套查詢來實現多對多,但嵌套查詢缺點較多,不推薦。
mybatis讓開發人員專註於資料庫的設計、sql語句的編寫,而不需要花費過多精力在jdbc的底層操作上。