工作中遇到的问题--使用DTO减少数据字段
Location中包含如下字段以及AMfgObject中关于创建信息的字段,然而有时使用并不需要传输那么多数据,则对其中字段进行过滤。
@Entity
@Table(name = "LOCATION")
@Where(clause="enabled=1") //Used for logical delete, disabled objects are always hidden
public class Location extends AMfgObject implements Serializable {
/** serialVersionUID */
    private static final long serialVersionUID = -5040870439658490644L;
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "LOCATION_ID", nullable = false, unique = true)
    private Long id;
    
    @NotBlank
    @Column(name = "CODE", nullable = false, unique = true)
    private String code;
    
    @NotBlank
    @Column(name = "NAME", nullable = false)
    private String name;
    
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "TYPE", nullable = false)
    private LocationType type;
/**
     * Empty constructor
     */
    public Location() {}
    
    @Override
    public void editFrom(AMfgObject object) {
        if (object == null)
            return;
        Location location = (Location)object;
        this.code = location.getCode();
        this.name = location.getName();
        this.type = location.getType();
    }
    
    @Override
    public String toString() {
        return getCode().concat(" - ").concat(getName());
    }
    
    public Long getId() {
        return id;
    }
public void setId(Long id) {
        this.id = id;
    }
public String getCode() {
        return code;
    }
public void setCode(String code) {
        this.code = code;
    }
public String getName() {
        return name;
    }
public void setName(String name) {
        this.name = name;
    }
public LocationType getType() {
        return type;
    }
public void setType(LocationType type) {
        this.type = type;
    }
    
}
创建DTO类,记录要传输的字段:
/**
 * Location info DTO holding basic location info for UI usage
 * @author damien
 *
 */
public class LocationInfoDTO  implements Serializable {
/** serialVersionUID */
    private static final long serialVersionUID = 2000078932471290548L;
/** Location id */
    private Long id;
/** Location code */
    private String code;
    
    /** Location name */
    private String name;
/**
     * Empty constructor
     */
    public LocationInfoDTO() {}
    
    /**
     * Constructor
     */
    public LocationInfoDTO(final Location location) {
        if (location != null) {
            id = location.getId();
            code = location.getCode();
            name = location.getName();
        }
    }
    
    public Long getId() {
        return id;
    }
public void setId(Long id) {
        this.id = id;
    }
public String getCode() {
        return code;
    }
public void setCode(String code) {
        this.code = code;
    }
public String getName() {
        return name;
    }
public void setName(String name) {
        this.name = name;
    }
    
}
在Service中对输出样式进行转换:
@Override
    @Transactional(readOnly = true)
    public List<LocationInfoDTO> getLocationInfoList() {
        List<LocationInfoDTO> locations = new ArrayList<LocationInfoDTO>();
        locationRepository.findAll().forEach(location -> locations.add(new LocationInfoDTO(location)));
        return locations;
    }
经过这样的步骤就可以减少传输数据的量了。
工作中遇到的问题--使用DTO减少数据字段的更多相关文章
- KETTLE4个工作中有用的复杂实例--1、数据定时自动(自动抽取)同步作业
		
今天呕心沥血花了8个小时给大家带来kettle工作中最常见的四种复杂实例,90%的项目用到这4种实例都可以解决. 4种实例种还有2种通用kettle工具,使用这两种通用工具实例,可以直接修改相应的配置 ...
 - mysql中与 in 相反的语句 find_in_set('数据',字段名)
		
在 mysql 中,我们经常用 in 来查询众多数据中是否有数据表字段中的值: 如果我们在数据表的字段中添加了很多值,然后查询某个值是否是这个字段中众多值的一个时可以用 find_in_set('数据 ...
 - js--前端开发工作中常见的时间处理问题
		
前言 在前端开发工作中,服务端返回的时间数据或者你传递给服务端的时间参数经常会遇到时间格式转换及处理问题.这里分享一些我收集到的一些处理方法,方便日后工作中快速找到.先附上必须了解的知识内置对象传送门 ...
 - JS单例模式在工作中的使用
		
为了尽可能的减少全局变量的污染,在写js的时候可以采用单例模式,形式如下: 比如有一个js叫demo.js,那么我们可以在js里这样写: var demo = {} 这样做的目的是将整个js当成一个对 ...
 - 工作中遇到的99%SQL优化,这里都能给你解决方案
		
前几篇文章介绍了mysql的底层数据结构和mysql优化的神器explain.后台有些朋友说小强只介绍概念,平时使用还是一脸懵,强烈要求小强来一篇实战sql优化,经过周末两天的整理和总结,sql优化实 ...
 - 工作中常见的hive语句总结
		
hive的启动: 1.启动hadoop2.开启 metastore 在开启 hiveserver2服务nohup hive --service metastore >> log.out 2 ...
 - 工作中SQL语句的优化
		
在我们的工作中,数据是很多的,这是我常见问题遇到的问题做了简短总结. 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 w ...
 - 关于提BUG的一点思考以及工作中总结的规范
		
在测试的工作中,提BUG是日常工作. 以前自己为了省事,省时,仅仅是截图,在图片上注明一下问题,就放到BUG库中了. 现在发现这样会造成开发的时间的浪费,增加了沟通成本. 对于BUG,当发现了异常时, ...
 - 工作中常用到的Java集合类有哪些?
		
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y Java集合是我认为在Java基础中最最重要的知 ...
 
随机推荐
- [开发笔记]-使用jquery获取url及url参数的方法
			
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下: window.location.href; 其实只是用到了javasc ...
 - sql 行专列 列转行 普通行列转换
			
转载:http://www.cnblogs.com/newwind521/archive/2010/11/25/1887203.html sql 行专列 列转行 普通行列转换 /* 标题:普通行列转换 ...
 - 恢复drop数据
			
select * from recyclebin r where r.original_name = 'MSM_EXAINVITEBIDSCHEMEHEAD' ; flashback table MS ...
 - Linux访问Windows磁盘实现共享
			
业务需求说明:公司在部署hadoop集群和DB server与SAN存储,公司的想法是前端通过DB Server能够将非结构化的数据能放进SAN存储当中,而hadoop集群也能够访问这个SAN存储.因 ...
 - 《java版进制转换》
			
import java.util.Scanner; class 十进制转成十六进制_2 { public static void main(String[] args) { int num = 0; ...
 - 码表由来:ascll码-Gbk2312-GBK-Unicode-UTF-8
			
码表ascll码-Gbk2312-GBK-Unicode-UTF-8, ascll是基本的标准码表,GB2312是中文码表,GBK是扩展之后的码表,Unicode是国际通用码表,UTF-8是优化后的U ...
 - libimobiledevice安装步骤
			
https://github.com/libimobiledevice/libimobiledevice libimobiledevice安装指南,你还不知道libimobiledevice为何物,赶 ...
 - mycat启动后,用Navicat Premium 连接报 "2013"
			
最近在学习mycat,启动后,用Navicat Premium 连接报 "2013" Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...
 - resolve some fragment exception
			
1.android fragment not attached to activity http://blog.csdn.net/walker02/article/details/7995407 if ...
 - hdu 2022
			
Ps:麻蛋...第一次想得太复杂了..用字符串组来存.越弄越傻逼...后来用int就行了... 代码: #include "stdio.h"#include "stdli ...