标签:
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;
}
经过这样的步骤就可以减少传输数据的量了。
标签:
原文地址:http://www.cnblogs.com/ly-radiata/p/4792604.html