• 工作中遇到的问题--使用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;
        }

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

  • 相关阅读:
    html5 表單元素
    html5 表單輸入類型
    html5 服務器發送事件
    html5 web workers
    html5應用緩存
    html5 sessionStorage VS loaclStorage
    html5地理定位
    html5 画布和SVG的差别
    html5 SVG
    SQL-22 统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4792604.html
Copyright © 2020-2023  润新知