1.编写接口
//根据套餐ID查询套餐详情(套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息)
public Setmeal findById(int id) {
return setmealDao.findById(id);
}
2.mabatis编写
<!--根据套餐ID查询套餐详情(包含套餐基本信息、检查组信息、检查项信息)-->
<select id="findById" parameterType="int" resultMap="findByIdResultMap">
select * from t_setmeal where id = #{id}
</select>
3.SetmealDao.xml编写findByIdResultMap
3.1实体类
public class Setmeal implements Serializable {
private Integer id;
private String name;
private String code;
private String helpCode;
private String sex;//套餐适用性别:0不限 1男 2女
private String age;//套餐适用年龄
private Float price;//套餐价格
private String remark;
private String attention;
private String img;//套餐对应图片存储路径
private List<CheckGroup> checkGroups;//体检套餐对应的检查组,多对多关系
<resultMap id="baseResultMap" type="com.itheima.pojo.Setmeal">
<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="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
</resultMap>
<resultMap id="findByIdResultMap" type="com.itheima.pojo.Setmeal" extends="baseResultMap">
<!--多对多映射-->
<collection
property="checkGroups" //参数
javaType="java.util.ArrayList" //参数类型
ofType="com.itheima.pojo.CheckGroup" //对应实体类名称
select="com.itheima.dao.CheckGroupDao.findCheckGroupById"//对应CheckGroupDao.xml下的findCheckGroupById方法
column="id"//传递的参数
>
</collection>
</resultMap>
4.CheckGroupDao.xml编写findCheckGroupById
4.1实体类
public class CheckGroup implements Serializable {
private Integer id;//主键
private String code;//编码
private String name;//名称
private String helpCode;//助记
private String sex;//适用性别
private String remark;//介绍
private String attention;//注意事项
private List<CheckItem> checkItems;//一个检查组合包含多个检查项
4.2编写findCheckGroupById
<resultMap id="baseResultMap" 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"/>
</resultMap>
<resultMap id="findByIdResultMap" type="com.itheima.pojo.CheckGroup" extends="baseResultMap">
<!--检查组和检查项多对多关联查询-->
<collection property="checkItems"
ofType="com.itheima.pojo.CheckItem"
javaType="java.util.ArrayList" //参数类型
column="id"
select="com.itheima.dao.CheckItemDao.findCheckItemById"//对应CheckItemDao.xml 下面findCheckItemById
></collection>
</resultMap>
<!--根据套餐ID查询关联的检查组详情-->
<select id="findCheckGroupById" parameterType="int" resultMap="findByIdResultMap">
select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{setmeal_id})
</select>
5编写CheckItemDao.xml
<!--根据检查组ID查询关联的检查项-->
<select id="findCheckItemById" parameterType="int" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem
where id
in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id})
</select>