mybatis collection 一对多映射返回的对象为空的处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011267841/article/details/81328547

    我这里是一个任务对应多个用例,是一对多的关系,直接上代码:

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 对应的是数据库查询出来的字段的别名,并不是数据库对应的字段名!!!,这样查出的对象就不为空了!

猜你喜欢

转载自blog.csdn.net/u011267841/article/details/81328547
今日推荐