ModelMapper,将实体列表映射到 DTO 对象列表
ModelMapper, mapping list of Entites to List of DTO objects
我正在使用 Spring MVC 框架编写简单的博客 Web 应用程序。我愿意为我的应用程序添加 DTO
层。
我决定使用 ModelMapper 框架将 Entity
对象转换为我视图中使用的 DTO
对象。
我只有一个问题。
在我的主页上,我显示了我博客上的帖子列表。在我看来,它只是 Post
(实体)对象的列表。我想更改它以将 PostDTO
对象列表传递到我的视图。有什么方法可以通过单个方法调用将 Post
个对象的 List
映射到 PostDTO
个对象的 List
?
我正在考虑编写 converter 来转换它,但我不确定这是一个好方法。
此外,我在其他几个地方使用 Lists
或 Entities
,例如管理面板或在我页面上的每个 Post 下面发表评论。
Link 到 GitHub 存储库中我的应用代码:repository
您可以创建 util class:
public class ObjectMapperUtils {
private static final ModelMapper modelMapper;
/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
/**
* Hide from public usage.
*/
private ObjectMapperUtils() {
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
并根据您的需要使用它:
List<PostDTO> listOfPostDTO = ObjectMapperUtils.mapAll(listOfPosts, PostDTO.class);
考虑到您有一个 Post 实体列表 (postEntityList
) 和一个 PostDTO class,您可以尝试以下操作:
使用以下导入获得所需的结果
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import java.lang.reflect.Type;
使用下面的代码
Type listType = new TypeToken<List<PostDTO>>(){}.getType();
List<PostDTO> postDtoList = modelmapper.map(postEntityList,listType);
既然要将Entity转Dto,可以试试下面的
List<PostDTO> entityToDto = modelMapper.map(postEntity, new TypeToken<List<PostDTO>>(){}.getType());
假设您正在从数据库中读取数据,并希望将数据库中的实体转换为 DTO
服务层可能与控制器分离(总是最好的方法),然后这样做:
服务层代码:
@Override
public List<PostDTO> convertEntityToDTOList( ){
List<Post> post = postRepository.findAll();
Type listType = new TypeToken<List<PostDTO>>(){}.getType();
List<PostDTO> postDTOList = modelMapper.map(post, listType);
return postDTOList;
}
然后在您的控制器中添加:
public ResponseEntity<?> list(){
List<PostDTO> postDTOList = iPost.convertEntityToDTOList();
return new ResponseEntity<>(postDTOList, HttpStatus.OK);
}
注意iPost是定义方法的接口。
就是这样:
public interface iPost {
List<PostDTO> convertEntityToDTOList();
}
感谢@André Carvalho
尝试以下简单方法:
List<PostDTO> postDtoList = Arrays.asList(modelMapper.map(postEntityList, PostDTO[].class));
我正在使用 Spring MVC 框架编写简单的博客 Web 应用程序。我愿意为我的应用程序添加 DTO
层。
我决定使用 ModelMapper 框架将 Entity
对象转换为我视图中使用的 DTO
对象。
我只有一个问题。
在我的主页上,我显示了我博客上的帖子列表。在我看来,它只是 Post
(实体)对象的列表。我想更改它以将 PostDTO
对象列表传递到我的视图。有什么方法可以通过单个方法调用将 Post
个对象的 List
映射到 PostDTO
个对象的 List
?
我正在考虑编写 converter 来转换它,但我不确定这是一个好方法。
此外,我在其他几个地方使用 Lists
或 Entities
,例如管理面板或在我页面上的每个 Post 下面发表评论。
Link 到 GitHub 存储库中我的应用代码:repository
您可以创建 util class:
public class ObjectMapperUtils {
private static final ModelMapper modelMapper;
/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
/**
* Hide from public usage.
*/
private ObjectMapperUtils() {
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
并根据您的需要使用它:
List<PostDTO> listOfPostDTO = ObjectMapperUtils.mapAll(listOfPosts, PostDTO.class);
考虑到您有一个 Post 实体列表 (postEntityList
) 和一个 PostDTO class,您可以尝试以下操作:
使用以下导入获得所需的结果
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import java.lang.reflect.Type;
使用下面的代码
Type listType = new TypeToken<List<PostDTO>>(){}.getType();
List<PostDTO> postDtoList = modelmapper.map(postEntityList,listType);
既然要将Entity转Dto,可以试试下面的
List<PostDTO> entityToDto = modelMapper.map(postEntity, new TypeToken<List<PostDTO>>(){}.getType());
假设您正在从数据库中读取数据,并希望将数据库中的实体转换为 DTO
服务层可能与控制器分离(总是最好的方法),然后这样做:
服务层代码:
@Override
public List<PostDTO> convertEntityToDTOList( ){
List<Post> post = postRepository.findAll();
Type listType = new TypeToken<List<PostDTO>>(){}.getType();
List<PostDTO> postDTOList = modelMapper.map(post, listType);
return postDTOList;
}
然后在您的控制器中添加:
public ResponseEntity<?> list(){
List<PostDTO> postDTOList = iPost.convertEntityToDTOList();
return new ResponseEntity<>(postDTOList, HttpStatus.OK);
}
注意iPost是定义方法的接口。 就是这样:
public interface iPost {
List<PostDTO> convertEntityToDTOList();
}
感谢@André Carvalho
尝试以下简单方法:
List<PostDTO> postDtoList = Arrays.asList(modelMapper.map(postEntityList, PostDTO[].class));