Mybatis學習第19節 -- 嵌套查詢一對多的配置
阿新 • • 發佈:2019-03-17
soc zab class namespace github npr ner arraylist 結果 一對多和多對多是一樣的.
而多對多是指 A對於B來說是一對多的關系, 同時B對於A來說也是一對多的關系, 互為一對多,即為多對多.
比如說一個標簽下面有多篇文章,一篇文章也可能有多個標簽
Shop實體類設計
Product實體類設計
ProductMapper接口
Product映射文件
測試使用Product
測試Product
略
ShopMapper配置文件
id, result, association, collection的順序不能錯, 因為有Mapper對應的構造器的原因, association的column屬性用來給select指定的方法進行參數傳入, collection也是一樣
List<Product> productList;
|
public class Product implements Serializable{
|
List<Product> getProductListByShopID(Integer id);
|
<?xml version="1.0" encoding="UTF-8" ?>
|
public class ProductMapperTest {
|
<resultMap id="simpleResultMap" type="Shop">
|
測試查詢Shop中的所有商品
@Test
|
測試結果
Shop{id=29, ownerId=1, area=Area{id=3, name=‘長治學院‘, priority=2, createTime=null, lastEditTime=null}, categoryId=22, name=‘暴漫奶茶店‘, desc=‘過來喝喝就知道啦,你是我的奶茶‘, addr=‘西苑1號‘, phone=‘1211334565‘, image=‘/upload/images/item/shop/29/2017092601054939287.jpg‘, priority=40, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘, productList=[Product{id=1, name=‘大黃人‘, desc=‘我是大黃人‘, imgAddr=‘upload/images/item/shop/29/2017092601204036435.jpg‘, normalPrice=‘2‘, promotionPrice=‘1‘, priority=100, createTime=Tue Sep 26 09:20:40 CST 2017, lastEditTime=Tue Sep 26 09:20:40 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=2, name=‘小黃人‘, desc=‘我是小黃人‘, imgAddr=‘upload/images/item/shop/29/2017092601212211185.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=90, createTime=Tue Sep 26 09:21:22 CST 2017, lastEditTime=Tue Sep 26 09:21:22 CST 2017, enableStatus=1, CategoryID=2, shopID=29} , Product{id=3, name=‘暴漫人‘, desc=‘開心了‘, imgAddr=‘upload/images/item/shop/29/2017092601220059819.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=80, createTime=Tue Sep 26 09:22:00 CST 2017, lastEditTime=Tue Sep 26 09:22:00 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=4, name=‘宇宙第一‘, desc=‘宇宙無敵‘, imgAddr=‘upload/images/item/shop/29/2017092601224389939.jpg‘, normalPrice=‘5‘, promotionPrice=‘2‘, priority=70, createTime=Tue Sep 26 09:22:43 CST 2017, lastEditTime=Tue Sep 26 09:22:43 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=5, name=‘眼凸凸‘, desc=‘宇宙無敵‘, imgAddr=‘upload/images/item/shop/29/2017092601231570458.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=60, createTime=Tue Sep 26 09:23:15 CST 2017, lastEditTime=Tue Sep 26 09:23:15 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=6, name=‘笑瞇瞇‘, desc=‘笑瞇瞇 甜蜜蜜‘, imgAddr=‘upload/images/item/shop/29/2017092601234922140.jpg‘, normalPrice=‘2‘, promotionPrice=‘2‘, priority=50, createTime=Tue Sep 26 09:23:49 CST 2017, lastEditTime=Tue Sep 26 09:23:49 CST 2017, enableStatus=1, CategoryID=3, shopID=29} ]} |
Mybatis學習第19節 -- 嵌套查詢一對多的配置