1. 程式人生 > >Mybatis學習第19節 -- 嵌套查詢一對多的配置

Mybatis學習第19節 -- 嵌套查詢一對多的配置

soc zab class namespace github npr ner arraylist 結果

一對多和多對多是一樣的. 而多對多是指 A對於B來說是一對多的關系, 同時B對於A來說也是一對多的關系, 互為一對多,即為多對多. 比如說一個標簽下面有多篇文章,一篇文章也可能有多個標簽 Shop實體類設計
List<Product> productList;
Product實體類設計
public class Product implements Serializable{

Integer id;
String name;
String desc;
String imgAddr;
String normalPrice;
String promotionPrice;
Integer priority;
Date createTime;
Date lastEditTime;
Integer enableStatus;
ProductMapper接口
List<Product> getProductListByShopID(Integer id);
Product映射文件
<?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="io.github.coinsjack.dao.ProductMapper">
<cache/>
<resultMap id="productResultMap" type="Product">
<result column="product_id" property="id"/>
<result column="product_name" property="name"/>
<result column="product_desc" property="desc"/>
<result column="img_addr" property="imgAddr"/>
<result column="normal_price" property="normalPrice"/>
<result column="promotion_price" property="promotionPrice"/>
<result column="create_time" property="createTime"/>
<result column="last_edit_time" property="lastEditTime"/>
<result column="enable_status" property="enableStatus"/>
<result column="product_category_id" property="categoryID"/>
<result column="shop_id" property="shopID"/>
</resultMap>

<select id="getProductListByShopID" resultMap="productResultMap">
select * from tb_product
WHERE `shop_id` = #{id};
</select>
</mapper>
測試使用Product
public class ProductMapperTest {
@Test
public void test_getProductByID() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
ProductMapper productMapper= sqlSession.getMapper(ProductMapper.class);

System.out.printf("查詢結果: %s%n", productMapper.getProductListByShopID(29));;
}
}
測試Product 略 ShopMapper配置文件
<resultMap id="simpleResultMap" type="Shop">
<id column="shop_id" property="id" ></id>
<result column="owner_id" property="ownerId" ></result>
<result column="shop_category_id" property="categoryId" ></result>
<result column="shop_name" property="name"></result>
<result column="shop_desc" property="desc"></result>
<result column="shop_addr" property="addr"></result>
<result column="phone" property="phone"></result>
<result column="shop_img" property="image"></result>
<result column="priority" property="priority"></result>
<result column="create_time" property="createTime"></result>
<result column="last_edit_time" property="lastEditTime"></result>
<result column="enable_status" property="enableStatus"></result>
<association property="area" column="area_id" javaType="Area"
select="io.github.coinsjack.dao.AreaMapper.getAreaById"/>
<collection property="productList" column="shop_id"
select="io.github.coinsjack.dao.ProductMapper.getProductListByShopID" javaType="ArrayList" ofType="Product" >
</collection>
</resultMap>
id, result, association, collection的順序不能錯, 因為有Mapper對應的構造器的原因, association的column屬性用來給select指定的方法進行參數傳入, collection也是一樣

測試查詢Shop中的所有商品

@Test
public void testGetShopById() {
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
System.out.println(mapper.getShopById(29));
session.close();
}

測試結果

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節 -- 嵌套查詢一對多的配置