MyBatis 关联查询的实现:多对多
2020-01-08 16:06:42来源:博客园 阅读 ()
MyBatis 关联查询的实现:多对多
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}]}
也可以使用嵌套查询来实现多对多,但嵌套查询缺点较多,不推荐。
原文链接:https://www.cnblogs.com/chy18883701161/p/12167005.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 天哪!手动编写mybatis雏形竟然这么简单 2020-06-06
- java实现分页查询 2020-06-05
- MyBatis整合双数据源 2020-06-04
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash