需求:实际:
查询setmeal 的全部信息
实体类中不仅有基本数据还有checkGroup的集合
其中checkGroup中的属性又有checkItem的集合
查询的setmeal对象中 需要包含一个或多个checkGroup 其中一个checkGroup又有多个checkItem
根据setmeal的id查询
就进行了嵌套查询
具体逻辑:
在查询setmeal返回时 重新给返回的数据进行命名赋值 原本的resultType是让它自己内部进行封装成你指定的类 resultMap是可以进行指定类型和进行内部数据的调整
其他的名字赋值不用变还是原来的就行
在checkGroup集合那个属性上 进行数据改变 column是发送携带的参数 在引用的执行select时可以使用 然后就将checkgroup集合的值指定为引用sql的返回值
<!--property是实体类名 oftype 是集合中属性的类 column里面是传递的数据 select是执行的语句这里直接指向了一个select方法,去执行那个dao中的那个方法-->
//集合进行分离
<collection
property="checkGroups"
ofType="com.itheima.pojo.CheckGroup"
column="id"
select="com.itheima.dao.CheckGroupDao.findCheckGroupById"
>
进入checkgroupdao执行被引用的方法
执行的是
select *
from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id});
根据传来的setmeal的属性id查询setmeal和checkgroup的中间表 查询符合该id的checkgroup的ids
它Id的集合 然后在查询checkgroupid进行匹配ids的chectGroup集合
在返回时再进行一个resultMap
将checkItem集合进行一下包装
将这个属性赋值为该sql的返回值
select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id =#{id})
返回一个checkItem的集合 子查询为 跟携带的checkgroup的属性Id 查询中间表 得到checkitem的Id集合 然后在进行id匹配查询(in) 得到checkitem集合进行返回
总结:
倒叙
查询出checkitem集合返回给一个checkgroup
查询出checkgroup集合返回给setmeal
1:SetMealDao:
mapper
/*
* 嵌套多表
* */
Setmeal findById(Integer id);
xml
<!--嵌套多表查询-->
<resultMap id="setmealResultMap" type="com.itheima.pojo.Setmeal">
<id property="id" column="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
<!--property是实体类名 oftype 是集合中属性的类 column里面是传递的数据 select是执行的语句这里直接指向了一个select方法,去执行那个dao中的那个方法-->
//集合进行分离
<collection
property="checkGroups"
ofType="com.itheima.pojo.CheckGroup"
column="id"
select="com.itheima.dao.CheckGroupDao.findCheckGroupById"
>
</collection>
</resultMap>
<select id="findById" resultMap="setmealResultMap">
select *
from t_setmeal where id=#{id};
</select>
2:checkGroupDao:
不用写mapper
直接xml
<resultMap id="findByIdResultMap" type="com.itheima.pojo.CheckGroup">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<collection property="checkItems"
ofType="com.itheima.pojo.CheckItem"
column="id"
select="com.itheima.dao.CheckItemDao.findCheckItemById"
></collection>
</resultMap>
<!--多表联合查询-->
<select id="findCheckGroupById" resultMap="findByIdResultMap">
select *
from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id});
</select>
3:checkItemDao:
不用mapper直接xml
<!--多表查询 -->
<select id="findCheckItemById" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id =#{id})
</select>