MyBaits_Day04
第一节、作业
<!-- 对getAllCateAndGoods做实现-->
<select id="getAllCateAndGoods" resultMap="cateAndGoods">
SELECT * FROM category;
</select>
<!-- type表示resultMap是在封装谁的对象 -->
<resultMap type="category" id="cateAndGoods">
<id property="cid" column="cid"/>
<!-- allGoods是一个list集合 myBatis不能自动封装-->
<!--对应list类型 我们用collection -->
<collection property="allGoods" ofType="goods" select="getGoodsByCid" column="cid"></collection>
</resultMap>
<select id="getGoodsByCid" resultType="goods">
SELECT * FROM goods WHERE cid=#{cid};
</select>
private int cid;
private String cname;
//每一个分类下对应的商品
//分析:一个商品分类下面有多个商品--一对多--一个分类下有多个商品(多个-集合)
private List<Goods> allGoods;
第二节、MyBatis做级联查询的时候的懒加载
什么是懒加载:按需加载
场景:我页面上只需要显示分类,当用户选中某个分类的时候,才去加载商品
版本一:(没有开启懒加载)每次页面进来,只要调用接口,会查询分类同时也查询分类下的商品
解决:开启懒加载
方法一:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u8bTsIQd-1631707125142)(C:\Users\王元元\AppData\Roaming\Typora\typora-user-images\image-20210904192103279.png)]
方法二:
<collection property="allGoods" ofType="goods" select="getGoodsByCid" column="cid" fetchType=