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减少数据字段的更多相关文章

  1. KETTLE4个工作中有用的复杂实例--1、数据定时自动(自动抽取)同步作业

    今天呕心沥血花了8个小时给大家带来kettle工作中最常见的四种复杂实例,90%的项目用到这4种实例都可以解决. 4种实例种还有2种通用kettle工具,使用这两种通用工具实例,可以直接修改相应的配置 ...

  2. mysql中与 in 相反的语句 find_in_set('数据',字段名)

    在 mysql 中,我们经常用 in 来查询众多数据中是否有数据表字段中的值: 如果我们在数据表的字段中添加了很多值,然后查询某个值是否是这个字段中众多值的一个时可以用 find_in_set('数据 ...

  3. js--前端开发工作中常见的时间处理问题

    前言 在前端开发工作中,服务端返回的时间数据或者你传递给服务端的时间参数经常会遇到时间格式转换及处理问题.这里分享一些我收集到的一些处理方法,方便日后工作中快速找到.先附上必须了解的知识内置对象传送门 ...

  4. JS单例模式在工作中的使用

    为了尽可能的减少全局变量的污染,在写js的时候可以采用单例模式,形式如下: 比如有一个js叫demo.js,那么我们可以在js里这样写: var demo = {} 这样做的目的是将整个js当成一个对 ...

  5. 工作中遇到的99%SQL优化,这里都能给你解决方案

    前几篇文章介绍了mysql的底层数据结构和mysql优化的神器explain.后台有些朋友说小强只介绍概念,平时使用还是一脸懵,强烈要求小强来一篇实战sql优化,经过周末两天的整理和总结,sql优化实 ...

  6. 工作中常见的hive语句总结

    hive的启动: 1.启动hadoop2.开启 metastore 在开启 hiveserver2服务nohup hive --service metastore >> log.out 2 ...

  7. 工作中SQL语句的优化

    在我们的工作中,数据是很多的,这是我常见问题遇到的问题做了简短总结. 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 w ...

  8. 关于提BUG的一点思考以及工作中总结的规范

    在测试的工作中,提BUG是日常工作. 以前自己为了省事,省时,仅仅是截图,在图片上注明一下问题,就放到BUG库中了. 现在发现这样会造成开发的时间的浪费,增加了沟通成本. 对于BUG,当发现了异常时, ...

  9. 工作中常用到的Java集合类有哪些?

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y Java集合是我认为在Java基础中最最重要的知 ...

随机推荐

  1. EL表达式中获取list长度

    在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...

  2. 大学生成绩管理系统(C语言)

    功能:成绩管理系统包含了学生的全部信息,每个学生是一个记录,包括学号,姓名,性别,班级,各科成绩(语数外). 系统功能: 1.信息录入——录入学生信息: 2.信息输出——显示所有信息: 3.信息查询— ...

  3. 自定义的dialog

    自定义的dialog  其中包含置顶 删除 和取消 下面的是BaseDialog package com.free.csdn.view.dialog; import android.app.Dialo ...

  4. Sketchup+ArcGIS三维建模与管理

    一.软件安装及其说明 1.需要安装的软件及其安装: 这份报告主要涉及到的有三个需要安装的软件ArcGIS9.3(或9.2) .Sketchup6.0和SketchUp6 ESRI 插件. ArcGIS ...

  5. [转]Android进程与线程基本知识

    转自:http://www.cnblogs.com/hanyonglu/archive/2012/04/12/2443262.html 本文介绍Android平台中进程与线程的基本知识. 很早的时候就 ...

  6. Hadoop中常用的InputFormat、OutputFormat(转)

    Hadoop中的Map Reduce框架依赖InputFormat提供数据,依赖OutputFormat输出数据,每一个Map Reduce程序都离不开它们.Hadoop提供了一系列InputForm ...

  7. SharePoint 2013 搜索体系结构

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,记录一下,算是自己对这部分知识的总结. Microsoft® SharePoint® Server ...

  8. memcpy的用法及实现

    memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中,返回dest所指内存地址的起始位置. #include <string.h&g ...

  9. jstl表达式替换某些字符

    转自:http://www.yiibai.com/jsp/jstl_function_replace.html fn:replace() 函数替换一个字符串与另一个字符串的所有匹配. 语法 fn:re ...

  10. 如何从SAP中查找BADI

    如何从SAP中查找BADI   如何从SAP中查找BADI http://blog.csdn.net/CompassButton/article/details/1231652 BADI作为SAP的第 ...