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

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

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 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

     
        
        
        
        
        
        
        
            
            
        
    
    
    

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修改:


    
        
        
        
        
        
        
        
            
            
            
        
    
    
    

看清楚了没

selectByPrimaryKey column 对应的是数据库查询出来的字段的别名,并不是数据库对应的字段名!!!,这样查出的对象就不为空了!

 

你可能感兴趣的:(mybatis,mapper映射)