zoukankan      html  css  js  c++  java
  • 工作中遇到的问题--使用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;
        }

    经过这样的步骤就可以减少传输数据的量了。

  • 相关阅读:
    后台取得非服务器控件的一种方法(Request.Form.GetKey(i))
    扩展jQuery键盘事件的几个基本方法(练习jQuery插件扩展)
    Javascript得到CheckBoxList的Value
    sql server的count(小技巧)
    oracle数据库约束条件删除、取消、启用
    iis7.0修改网站端口
    session模式和web园
    理解Session State模式+ASP.NET SESSION丢失FAQ (转)
    Gridview中生成的属性rules="all",在Firefox出现内线框解决办法
    一个类windows系统的效果图
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4792604.html
Copyright © 2011-2022 走看看