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. lucene4.7学习总结

    花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...

  2. Hello Hibernate

    Hibernate 一个框架; 一个 Java 领域的持久化框架; 一个 ORM 框架 ORM(Object/Relation Mapping): 对象/关系映射 –ORM的思想:将关系数据库中表中的 ...

  3. UVA 10970-Big Chocolate

    题目: 给你一块M*N的巧克力,问把它切成最小单元需要最少切几刀,分开的就不能一起切了. 分析: 每次切割只能多产生一个部分,分成M*N个部分,必然要切M*N-1刀. 一个长为m宽为n的长方形和m*n ...

  4. android studio只能全部提示设置

  5. JS 验证一组input框是否为空的方法

    function checkInput() { var $tr = $("#tb_confirmed .scrollContent").find("tr"); ...

  6. Allegro PCB SI (2)

    整理一下在电研院学的si (虽然彩超的si在频率15Mhz以上后,si是失真的.昨晚遇到孔大哥也是这样说的,板级仿真,要layout过硬,然后找到合适的top test point) Allegro ...

  7. Canopy使用教程 (3)

    1. 2. plot函数: plot默认生成是曲线图,可以通过kind参数生成其他的图形,可选的值为:line, bar, barh, kde, density, scatter. 散点图.使用kin ...

  8. sap 根据TOCE找 USER_EXIT

    *&---------------------------------------------------------------------* *& Report  ZUSER_EX ...

  9. 2016-1-7第一个完整APP 私人通讯录的实现 5:保存数据

    一:登陆界面 1):用户点击登陆按钮并成功登陆后,根据此时的开关情况选择是否保存数据,代码如下: "]) { [self performSegueWithIdentifier:@" ...

  10. C++ offsetof

    这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...