• 使用数据传输对象避免写多表关联查询


    @Service
    public class QuestionServiceImpl implements QuestionService {
    	@Autowired
    	private QuestionMapper questionMapper;
    	@Autowired
    	private UserMapper userMapper;
    
        @Override
        public List<QuestionDTO> findAll() {
        	List<QuestionDTO> questionDTOs = new ArrayList<>();
        	List<Question> questions = questionMapper.findAll();
        
        	User user = null;
        	QuestionDTO questionDTO = null;
        	for (Question question : questions) {
        		user = userMapper.findById(question.getPublisher());
        
        		questionDTO = new QuestionDTO();
        		BeanUtils.copyProperties(question, questionDTO);
        
        		// 为数据传输对象设置user
        		questionDTO.setUser(user);
        		questionDTOs.add(questionDTO);
        	}
        
        	return questionDTOs;
        }
    }
    

    Question.java

    @Data
    public class Question {
    	private Integer id;//
    	private String title;// varchar(50)
    	private String description;// text,
    	private Long gmtCreated;// bigint(20) DEFAULT NULL,
    	private Long gmtModified;//
    	private Integer publisher;// 问题发布者id
    	private Integer commentNum;// 评论数
    	private Integer viewNum;// 浏览数
    	private Integer likeNum;// 点赞数
    	private String tag;// 问题标签
    }
    

    传输对象

    package com.fei.dto;
    
    import com.fei.domain.User;
    
    import lombok.Data;
    
    @Data
    public class QuestionDTO {
    	private Integer id;//
    	private String title;// varchar(50)
    	private String description;// text,
    	private Long gmtCreated;// bigint(20) DEFAULT NULL,
    	private Long gmtModified;//
    	private Integer publisher;// 问题发布者id
    	private Integer commentNum;// 评论数
    	private Integer viewNum;// 浏览数
    	private Integer likeNum;// 点赞数
    	private String tag;// 问题标签
    	
    	// 数据传输对象,增加发布者User的id
    	private User user;
    }
    
  • 相关阅读:
    codec功能简介
    dtmf原理说明
    linux的vm.overcommit_memory的内存分配参数详解
    Hibernate与Sleep的区别
    简单的读写-simple_read_from_buffer
    linux delay sleep
    Linux系统上的popen()库函数
    Linux中popen函数的作用小结
    ulimit 命令详解
    LTE Cat1有什么用?基于4G LTE打造cat1,弥补NB-IoT和5G的空缺
  • 原文地址:https://www.cnblogs.com/zxfei/p/11741264.html
Copyright © 2020-2023  润新知