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

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

转载于:https://www.cnblogs.com/ly-radiata/p/4792604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Mybatis-Plus进行DTO的操作可以通过以下步骤实现: 1. 首先,确保已经引入了Mybatis-Plus的依赖,可以在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 创建DTO(Data Transfer Object)对象,用于封装查询结果。DTO对象通常包含与数据库表字段对应的属性,并提供相应的getter和setter方法。 3. 在Mapper接口使用@Mapper注解标识该接口为Mybatis的Mapper接口,并使用@MapperScan注解扫描Mapper接口所在的包。 4. 在Mapper接口定义查询方法,可以使用Mybatis-Plus提供的查询方法,也可以自定义SQL语句进行查询。在查询方法的参数使用@Param注解指定参数名称,以便在SQL语句引用。 5. 在Service层调用Mapper接口的查询方法,获取查询结果。 6. 将查询结果封装到DTO对象,并返回给前端或其他需要的地方。 总结起来,使用Mybatis-Plus进行DTO的操作主要包括创建DTO对象、定义Mapper接口的查询方法、在Service层调用查询方法并封装结果到DTO对象。这样可以实现使用Mybatis-Plus进行DTO的操作。 #### 引用[.reference_title] - *1* *3* [Mybatis-plus做连接查询的插件Mybatis-plus-join](https://blog.csdn.net/m0_67400973/article/details/126463252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Mybatis-Plus 开发提速器:mybatis-plus-generator-ui 你确定不了解一下?](https://blog.csdn.net/weixin_44421461/article/details/129483755)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值