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


    @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;
    }
    
  • 相关阅读:
    邮件发送的存储过程写法
    FormsAuthentication.GetRedirectUrl 方法
    视图性能优化——索引视图
    删除多字段的重复行保留最大最小行
    c#知识点
    sql存储过程exec执行字符串select 的区别
    UVALive5198 UVA512 Spreadsheet Tracking
    UVA129 HDU1627 Krypton Factor
    UVA129 HDU1627 Krypton Factor
    UVA10603 Fill
  • 原文地址:https://www.cnblogs.com/zxfei/p/11741264.html
Copyright © 2020-2023  润新知