錯誤展示
- 多表關聯查詢的返回結果集
<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中的屬性startTime與endTime和上面的startTime與endTime屬性名相同。
- 查詢語句展示
<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>
在junit中進行mapper層測試,原本數據庫中只有一條SpecialDay的記錄,並沒有關聯的WorkTime記錄,那么理想的結果是返回的VO對象中
specialDay相關的屬性有值,而List
[{"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值,結果與測試結果不符合。
解決方案
- 給workTime表查詢結果設置一個區分的別名,同時修改resultMap中
標簽下的對應的字段名(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>
- 相同測試數據如下:
[{"specialDateId":2,"name":"b","date":"2019-04-27","type":0,"startTime":"5:00","endTime":"17:00","workTimeList":[]}]
此次測試結果正確!workTimeList長度為0。