1.背景
三张表:
- 检查组checkgroup
- 检查项checkItem
CheckItem与CheckGroup是多对多的关系,所以引入t_checkgroup_checkItem作为关系表
- 套餐表setmeal:
套餐表与检查组为多对多的关系,所以引入一张新表t_setMeal_checkGroup:
在java中三张主表对应的实体类:
- ChekItem类
public class CheckItem implements Serializable {
private Integer id;//主键
private String code;//项目编码
private String name;//项目名称
private String sex;//适用性别
private String age;//适用年龄(范围),例如:20-50
private Float price;//价格
private String type;//检查项类型,分为检查和检验两种类型
private String remark;//项目说明
private String attention;//注意事项
//其他方法省略
}
- CheckGroup类;
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;//一个检查组合包含多个检查项
//其他方法省略
}
- SetMeal类
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;//体检套餐对应的检查组,多对多关系
//其他方法省略
}
2.目标
查询setmeal,一般查询时,我们只查询setmeal相关属性,不在乎属性checkGroups时,可以使用一般的resultType自动封装:
<select id="findById" resultTyoe="com.zj.pojo.SetMeal">
select * from t_setmeal where id=#{id}
<select>
这里的id为dao接口传递过来的参数id,例如:
public SetMeal findById(int id);
但是项目中还有很多情况要求查询SetMeal的同时将所关联的CheckGroup也查询出来,又因为CheckGroup中也关联了CheckItem,所以也要讲CheckItem查询出来,这就涉及到了连续查询三张表,我们此时就需要用到mybatis的ResultMap中的collection。
在SetMealDao.xml中:
<!-- 查询语句:用于查询SetMeal表-->
<select id="findDetailsById" parameterType="int" resultMap="findDetailsByIdResultMap">
select * from t_Setmeal where id=#{id}
</select>
<!--设置映射关系,这里主要设置除了CheckGroups以外的所有属性-->
<resultMap id="baseResultMap" type="com.zj.pojo.Setmeal">
<id property="id" column="id"></id>
<result column="name" property="name"></result>
<result column="code" property="code"></result>
<result column="helpCode" property="helpCode"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<result column="price" property="price"></result>
<result column="remark" property="remark"></result>
<result column="attention" property="attention"></result>
<result column="img" property="img"></result>
</resultMap>
<!--
extends: 继承另一个ResultMap的映射关系
在collection中:
propertis:实体中对应的成员名
ofType:对应的成员类别
column:使用之前查询的表中的哪一个属性作为select的传入参数
select :查询checkgroup需要的select语句(量表查询),可以写select语句所在xml的位置
-->
<resultMap id="findDetailsByIdResultMap" type="com.zj.pojo.Setmeal" extends="baseResultMap">
<!-- 多对多映射-->
<!-- 这里的id对应findDetailsById查询出来的Id-->
<collection property="checkGroups" ofType="com.zj.pojo.CheckGroup"
select="com.zj.dao.CheckGroupDao.findCheckGroupById" column="id"></collection>
</resultMap>
在SetMeal中需要提供com.zj.dao.CheckGroupDao.findCheckGroupById 实现查询CheckGroup,所以我们在CheckGroupdao.xml中编写:
<!--实现多对多两表查询的语句,这里的id由SetMeal中collection的culomn进行引入-->
<select id="findCheckGroupById" parameterType="int" resultMap="findCheckGroupByIdResultMap">
select * from t_checkgroup where id in (select checkgroup_Id from t_setmeal_checkgroup where setmeal_id=#{id})
</select>
<!--基本映射-->
<resultMap id="baseResultMap" type="com.zj.pojo.CheckGroup">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="code" column="code"></result>
<result property="helpCode" column="helpCode"></result>
<result property="sex" column="sex"></result>
<result property="remark" column="remark"></result>
<result property="attention" column="attention"></result>
</resultMap>
<!--关联查询Checkitem-->
<resultMap id="findCheckGroupByIdResultMap" type="com.zj.pojo.CheckGroup" extends="baseResultMap">
<collection property="checkItems" ofType="com.zj.pojo.CheckItem" column="id" select="com.zj.dao.CheckItemDao.findCheckItemById"></collection>
</resultMap>
在CheckGroupDao.xml中需要com.zj.dao.CheckItemDao.findCheckItemById 实现两表查询CheckItem,所以在CheckItemDao.xml中:
<select id="findCheckItemById" resultType="com.zj.pojo.CheckItem" parameterType="int">
select * from t_checkitem where id in (select checkItem_Id from t_checkgroup_checkitem where checkgroup_id=#{id})
</select>
就这样,通过ResultMap的collection实现了三张多对多关系的表实现的多变联查成功。重点关注collection作用就是为实体中有List<另一个实体> 这样的形式进行映射设定