xyz*_*xyz 16 java jpa spring-mvc
我在我的Web应用程序中使用Spring MVC
架构JPA
.
哪里可以手动将DTO转换为实体,反之亦然(不使用任何框架)?
Sab*_*han 19
这是一个已接受答案的旧问题,但是使用模型映射器API以简单的方式更新它.
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
使用此API,您可以避免手动设置器和getter,如接受的答案中所述.
在我看来,两个转换都应该在私有实用程序方法的帮助下在控制器上进行,并使用Java8流的映射(如果交换了DTO集合),如本文所示.
它应该发生在控制器上,因为DTO应该是独占的传输对象.我不会进一步减少我的DTO.
您可以在实体上编写服务和数据访问层,并在调用服务方法之前将DTO转换为实体,并在从控制器返回响应之前将实体转换为DTO.
我更喜欢这种方法,因为实体很少更改,并且可以根据需要从DTO添加/删除数据.
我想你问的是在哪里写整个实体 - > DTO转换逻辑.
喜欢你的实体
class StudentEntity {
int age ;
String name;
//getter
//setter
public StudentDTO _toConvertStudentDTO(){
StudentDTO dto = new StudentDTO();
//set dto values here from StudentEntity
return dto;
}
}
Run Code Online (Sandbox Code Playgroud)
你的DTO应该是这样的
class StudentDTO {
int age ;
String name;
//getter
//setter
public StudentEntity _toConvertStudentEntity(){
StudentEntity entity = new StudentEntity();
//set entity values here from StudentDTO
return entity ;
}
}
Run Code Online (Sandbox Code Playgroud)
你的控制器应该是这样的
@Controller
class MyController {
public String my(){
//Call the conversion method here like
StudentEntity entity = myDao.getStudent(1);
StudentDTO dto = entity._toConvertStudentDTO();
//As vice versa
}
}
Run Code Online (Sandbox Code Playgroud)
我建议另一种无需额外依赖的方法:
import org.springframework.beans.BeanUtils
...
BeanUtils.copyProperties(sourceObject, targetObject);
Run Code Online (Sandbox Code Playgroud)
如果属性类型和名称相同,则可用于将DTO转换为实体,反之亦然。
如果您想忽略某些字段,只需在后面添加它们targetObject
。
BeanUtils.copyProperties(sourceObj, targetObj, "propertyToIgnoreA", "propertyToIgnoreB", "propertyToIgnoreC");
Run Code Online (Sandbox Code Playgroud)
来源:http://appsdeveloperblog.com/dto-to-entity-and-entity-to-dto-conversion/
我认为这是最干净的方法。
小智 7
在我看来
它使您可以更好地控制进程,并且每次更改填充实体的某些逻辑时,您不必更改服务/持久性类.
我可以建议使用mapstruct库:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.2.0.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
例如,如果您有这样一个实体:
public class Entity {
private Integer status;
private String someString;
private Date startDate;
private Date endDate;
// SKIPPED
Run Code Online (Sandbox Code Playgroud)
和DTO:
public class Dto {
private Boolean status;
private String someString;
private Long startDate;
private Long endDate;
// SKIPPED
Run Code Online (Sandbox Code Playgroud)
然后可以通过以下方式在服务层中完成转换:
@Service
public class SomeServiceImpl implements SomeService {
@Autowired
SomeDao someDao;
@Autowired
SomeMapper someMapper;
public Dto getSomething(SomeRequest request) throws SomeException {
return someDao.getSomething(request.getSomeData())
.map(SomeMapper::mapEntityToDto)
.orElseThrow(() -> new SomeException("..."));
}
Run Code Online (Sandbox Code Playgroud)
映射器可以表示如下:
@Mapper
public interface SomeMapper {
@Mappings(
{@Mapping(target = "entity",
expression = "java(entity.getStatus() == 1 ? Boolean.TRUE : Boolean.FALSE)"),
@Mapping(target = "endDate", source = "endDate"),
@Mapping(target = "startDate", source = "startDate")
})
Dto mapEntityToDto(Entity entity);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33852 次 |
最近记录: |