mybatis collection 一對多對映返回的物件為空的處理
阿新 • • 發佈:2019-01-07
我這裡是一個任務對應多個用例,是一對多的關係,直接上程式碼:
package com.gameloft9.demo.dataaccess.model.task; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.gameloft9.demo.base.BaseEntity; import com.gameloft9.demo.dataaccess.model.biz.InterfaceMain; import lombok.Data; import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper=true) public class Task extends BaseEntity{ /** * */ private static final long serialVersionUID = 1L; /**任務編號*/ private String taskCode; /**介面名稱*/ private String taskName; /**起始時間YYYYmmDDHHmmss,每小時,周,月執行時候不能為空**/ private Date startTime; /**結束時間YYYYmmDDHHmmss*/ private Date endTime; /**週期型別 h:每小時;d:每天; w:每週 ;m:每月**/ private String cycleType; private ArrayList<InterfaceMain> interfaceUserCaseList; }
package com.gameloft9.demo.dataaccess.model.biz; import com.gameloft9.demo.base.BaseEntity; import lombok.Data; import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper=true) public class InterfaceMain extends BaseEntity{ /** * */ private static final long serialVersionUID = 1L; /**介面編號*/ private String interfaceCode; /**介面名稱*/ private String interfaceName; /**備註*/ private String bz; }
對映檔案XML
<resultMap type="com.gameloft9.demo.dataaccess.model.task.Task" id="taskInterface"> <id column="ID" property="id"></id> <result column="TASK_CODE" property="taskCode"></result> <result column="TASK_NAME" property="taskName"></result> <result column="START_TIME" property="startTime"></result> <result column="END_TIME" property="endTime"></result> <collection column="ID" property="interfaceUserCaseList" ofType="com.gameloft9.demo.dataaccess.model.biz.InterfaceMain"> <result column="INTERFACE_CODE" property="interfaceCode"></result> <result column="INTERFACE_NAME" property="interfaceName"></result> </collection> </resultMap> <select id="selectByPrimaryKey" resultType="com.gameloft9.demo.dataaccess.model.task.Task" resultMap="taskInterface"> select t0.ID as id, t0.TASK_CODE as taskCode, t0.TASK_NAME as taskName, t0.START_TIME as startTime, t0.END_TIME as endTime, t3.ID as tempID, t3.INTERFACE_NAME as interfaceName, t3.INTERFACE_CODE as interfaceCode from TASK t0 LEFT JOIN TASK_INTERFACE t1 ON t0.ID=t1.TASK_ID LEFT JOIN INTERFACE_USER_CASE t2 ON t1.INTERFACE_ID=t2.ID LEFT JOIN INTERFACE_MAIN t3 ON t3.ID=t2.INTERFACE_ID where t0.ID = #{id,jdbcType=VARCHAR} </select>
dao層
package com.gameloft9.demo.dataaccess.dao.task;
import com.gameloft9.demo.dataaccess.model.task.Task;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by zgb on 2018/06/28.
*/
public interface TaskMapper {
/**
* 查詢
* @param id
* @return
*/
Task selectByPrimaryKey(String id);
}
serverImpl層,此處介面層我不寫了!
package com.gameloft9.demo.service.impl.task;
import com.gameloft9.demo.service.api.task.TaskService;
import com.gameloft9.demo.service.beans.system.PageRange;
import com.gameloft9.demo.service.beans.task.TaskRequest;
import com.gameloft9.demo.service.beans.task.TaskResponse;
import com.gameloft9.demo.utils.Constants;
import com.gameloft9.demo.utils.UUIDUtil;
import com.gameloft9.demo.dataaccess.dao.task.TaskInterfaceMapper;
import com.gameloft9.demo.dataaccess.dao.task.TaskMapper;
import com.gameloft9.demo.dataaccess.model.task.Task;
import com.gameloft9.demo.dataaccess.model.task.TaskInterface;
import com.gameloft9.demo.mgrframework.utils.CheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
* Created by zgb on 2018/06/28.
*/
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {
@Autowired
TaskMapper dao;
/**
* 根據id獲取記錄
* @param id 主鍵
* */
public TaskResponse getById(String id){
CheckUtil.notBlank(id,"id為空");
TaskResponse taskResponse = new TaskResponse();
Task task = dao.selectByPrimaryKey(id);
taskResponse.setId(task.getId());
taskResponse.setTaskCode(task.getTaskCode());
taskResponse.setTaskName(task.getTaskName());
taskResponse.setCreateTime(task.getCreateTime());
taskResponse.setUpdateTime(task.getUpdateTime());
taskResponse.setStartTime(task.getStartTime());
taskResponse.setEndTime(task.getEndTime());
return taskResponse;
}
}
除錯的時候,task物件裡面屬性的值為空,怎麼解決,搞了好久,終於找到突破口,請看程式碼對映map的XML修改:
<resultMap type="com.gameloft9.demo.dataaccess.model.task.Task" id="taskInterface">
<id column="ID" property="id"></id>
<result column="taskCode" property="taskCode"></result>
<result column="taskName" property="taskName"></result>
<result column="startTime" property="startTime"></result>
<result column="endTime" property="endTime"></result>
<collection property="interfaceUserCaseList"
ofType="com.gameloft9.demo.dataaccess.model.biz.InterfaceMain">
<result column="tempID" property="id"></result>
<result column="interfaceCode" property="interfaceCode"></result>
<result column="interfaceName" property="interfaceName"></result>
</collection>
</resultMap>
<select id="selectByPrimaryKey" resultType="com.gameloft9.demo.dataaccess.model.task.Task" resultMap="taskInterface">
select
t0.ID id,
t0.TASK_CODE taskCode,
t0.TASK_NAME taskName,
t0.START_TIME startTime,
t0.END_TIME endTime,
t3.ID tempID,
t3.INTERFACE_CODE interfaceCode,
t3.INTERFACE_NAME interfaceName
from TASK t0
LEFT JOIN TASK_INTERFACE t1 ON t0.ID=t1.TASK_ID
LEFT JOIN INTERFACE_USER_CASE t2 ON t1.INTERFACE_ID=t2.ID
LEFT JOIN INTERFACE_MAIN t3 ON t3.ID=t2.INTERFACE_ID
where t0.ID = #{id,jdbcType=VARCHAR}
</select>
看清楚了沒
selectByPrimaryKey column 對應的是資料庫查詢出來的欄位的別名,並不是資料庫對應的欄位名!!!,這樣查出的物件就不為空了!