SSM学习08MyBatis关联映射多对多
Posted 2312947032zyk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM学习08MyBatis关联映射多对多相关的知识,希望对你有一定的参考价值。
和上一个相同还是<collection>是多对多映射,一个订单可能包含多个产品,而每个商品有可能出现在多个订单中,在数据库中这样的情况就需要一张中间表来维护。
1.在mybatis中建表:
1 create table tb_product( 2 id int(32) primary key auto_increment, 3 name varchar(32), 4 price double 5 ); 6 7 insert into tb_product values(‘1‘,‘Java基础入门‘,‘44.5‘); 8 insert into tb_product values(‘2‘,‘Java Web程序开发入门‘,‘38.5‘); 9 insert into tb_product values(‘3‘,‘SSM框架整合实战‘,‘50‘); 10 11 create table tb_ordersitem( 12 id int(32) primary key auto_increment, 13 orders_id int(32), 14 product_id int(32), 15 foreign key(orders_id) references tb_orders(id), 16 foreign key(product_id) references tb_product(id) 17 ); 18 19 insert into tb_ordersitem values(‘1‘,‘1‘,‘1‘); 20 insert into tb_ordersitem values(‘2‘,‘1‘,‘3‘); 21 insert into tb_ordersitem values(‘3‘,‘3‘,‘3‘);
可以看出tb_ordersitem中引用了tb_prduct和tb_orders中的主键作为外键。
Product:
1 package com.zyk.po; 2 3 import java.util.List; 4 5 public class Product { 6 private int id; 7 @Override 8 public String toString() { 9 return "Product [id=" + id + ", name=" + name + ", price=" + price + ", orders=" + orders + "]"; 10 } 11 private String name; 12 private Double price; 13 private List<Orders> orders; 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public Double getPrice() { 27 return price; 28 } 29 public void setPrice(Double price) { 30 this.price = price; 31 } 32 public List<Orders> getOrders() { 33 return orders; 34 } 35 public void setOrders(List<Orders> orders) { 36 this.orders = orders; 37 } 38 39 40 }
Order:
1 package com.zyk.po; 2 3 import java.util.List; 4 5 public class Orders { 6 private int id; 7 @Override 8 public String toString() { 9 return "Orders [id=" + id + ", productlist=" + productlist + ", number=" + number + "]"; 10 } 11 private List<Product> productlist; 12 private String number; 13 public List<Product> getProductlist() { 14 return productlist; 15 } 16 public void setProductlist(List<Product> productlist) { 17 this.productlist = productlist; 18 } 19 20 public int getId() { 21 return id; 22 } 23 public void setId(int id) { 24 this.id = id; 25 } 26 public String getNumber() { 27 return number; 28 } 29 public void setNumber(String number) { 30 this.number = number; 31 } 32 33 34 }
从两个javabean中可以看出:product中有order,order中有product
OrderMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.itheima.mapper.OrdersMapper"> 5 <select id="findOrdersWithProduct" parameterType="Integer" resultMap="OrdersWithProductResult"> 6 select o.*,p.id as pid,p.name,p.price 7 from tb_orders o,tb_product p,tb_ordersitem oi 8 where oi.orders_id=o.id 9 and oi.product_id=p.id 10 and o.id=#{id} 11 </select> 12 <resultMap type="Orders" id="OrdersWithProductResult"> 13 <id property="id" column="id"/> 14 <result property="number" column="number"/> 15 <collection property="productList" ofType="Product"> 16 <id property="id" column="pid"/> 17 <result property="name" column="name"/> 18 <result property="price" column="price"/> 19 </collection> 20 </resultMap> 21 </mapper>
测试类:
1 @Test 2 public void findOrdersAndPorductTest() { 3 SqlSession session = MybatisUtils.getSession(); 4 Orders orders = session.selectOne("com.itheima.mapper.OrdersMapper.findOrdersWithProduct",1); 5 System.out.println(orders); 6 //关闭SqlSession 7 session.close(); 8 }
结果:
以上是关于SSM学习08MyBatis关联映射多对多的主要内容,如果未能解决你的问题,请参考以下文章