一、错误展示
1.首先向大家展示多表关联查询的返回结果集
<resultMap id="specialdayAndWorktimeMap" type="com.hierway.resm.domain.manage.timeSchedule.SpecialDayWorkTimeVO">
<id column="special_date_id" property="specialDateId"/>
<result column="name" property="name"/>
<result column="date" property="date"/>
<result column="type" property="type"/>
<result column="start_time" property="startTime"/>
<result column="end_time" property="endTime"/>
<collection property="workTimeList" ofType="com.hierway.resm.domain.manage.timeSchedule.WorkTime">
<id column="work_time_id" property="workTimeId"/>
<result column="start_time" property="startTime"/>
<result column="end_time" property="endTime"/>
</collection>
</resultMap>
上面的映射中,返回结果类:SpecialDayWorkTimeVO中定义了7个属性,其中第7个属性是关联查询一对多的对象List<WorkTime>。可以看到在workTime中的属性startTime与endTime和上面的startTime与endTime属性名相同。
2.查询语句展示
<select id="getSpecialDayAndWorkTimesByPuId" resultMap="specialdayAndWorktimeMap">
SELECT sd.special_date_id,sd.name,sd.type,sd.date,sd.start_time,sd.end_time,wt.work_time_id,
wt.start_time,wt.end_time
FROM powerunit_specialday_relation AS psr
LEFT JOIN special_day AS sd ON psr.special_date_id = sd.special_date_id
LEFT JOIN specailday_worktime_relation AS swr ON sd.special_date_id = swr.special_date_id
LEFT JOIN work_time AS wt ON swr.work_time_id = wt.work_time_id
WHERE psr.pu_id = #{puId}
</select>
3.在junit中进行mapper层测试,原本数据库中只有一条SpecialDay的记录,并没有关联的WorkTime记录,那么理想的结果是返回的VO对象中specialDay相关的属性有值,而List<WorkTime>是一个长度为0的List。可是测试结果转为Json格式如下所示:
[{"specialDateId":2,"name":"b","date":"2019-04-27","type":0,"startTime":"5:00",
"endTime":"17:00","workTimeList":[{"workTimeId":null,"startTime":"5:00","endTime":"17:00"}]}]
可以看到,list中含有一条数据,且前面的属性,覆盖了后面具有相同属性名的WorkTime中的startTime与endTime值,结果与测试结果不符合。
二、解决方案
1.给workTime表查询结果设置一个区分的别名,同时修改resultMap中<collection>标签下的对应的字段名(column元素),展示如下:
<resultMap id="specialdayAndWorktimeMap" type="com.hierway.resm.domain.manage.timeSchedule.SpecialDayWorkTimeVO">
<id column="special_date_id" property="specialDateId"/>
<result column="name" property="name"/>
<result column="date" property="date"/>
<result column="type" property="type"/>
<result column="start_time" property="startTime"/>
<result column="end_time" property="endTime"/>
<collection property="workTimeList" ofType="com.hierway.resm.domain.manage.timeSchedule.WorkTime">
<id column="work_time_id" property="workTimeId"/>
<result column="wstart_time" property="startTime"/>
<result column="wend_time" property="endTime"/>
</collection>
</resultMap>
<!--List<SpecialDayWorkTimeVO> getSpecialDayAndWorkTimesByPuId(Integer puId);-->
<select id="getSpecialDayAndWorkTimesByPuId" resultMap="specialdayAndWorktimeMap">
SELECT sd.special_date_id,sd.name,sd.type,sd.date,sd.start_time,sd.end_time,wt.work_time_id,
wt.start_time AS wstart_time ,wt.end_time AS wend_time
FROM powerunit_specialday_relation AS psr
LEFT JOIN special_day AS sd ON psr.special_date_id = sd.special_date_id
LEFT JOIN specailday_worktime_relation AS swr ON sd.special_date_id = swr.special_date_id
LEFT JOIN work_time AS wt ON swr.work_time_id = wt.work_time_id
WHERE psr.pu_id = #{puId}
</select>
2.相同测试数据如下:
[{"specialDateId":2,"name":"b","date":"2019-04-27","type":0,"startTime":"5:00","endTime":"17:00","workTimeList":[]}]
此次测试结果正确!workTimeList长度为0。