2017-12-21 10 views
0

Ich schreibe einfache Blog-Webanwendung mit Spring MVC-Framework. Ich bin bereit, DTO Schicht meiner App hinzuzufügen.ModelMapper, Mapping-Liste von Entitäten zu Liste von DTO-Objekten

Ich entschied mich, ModelMapper Framework für die Konvertierung von Entity Objekte zu DTO Objekte in meinen Ansichten verwendet zu verwenden.

Ich habe nur ein Problem. Auf meiner Hauptseite zeige ich eine Liste von Posts in meinem Blog. Aus meiner Sicht ist es nur eine Liste von Post (Entity) Objekte. Ich möchte es ändern, um eine Liste von PostDTO Objekte zu meiner Ansicht zu übergeben. Gibt es eine Möglichkeit List von Post Objekte zu List von PostDTO Objekt mit einem einzigen Methodenaufruf zuzuordnen? Ich dachte über das Schreiben von converter, die dies konvertieren wird, aber ich bin mir nicht sicher, es ist ein guter Weg, es zu tun.

Auch ich verwende Lists von Entities in einigen mehr Orten wie administrative Panel oder Kommentar unter jedem Beitrag auf meiner Seite.

Link zu Code meiner App auf GitHub-Repository: repository

Antwort

1

Sie util Klasse erstellen:

public class ObjectMapperUtils { 

    private static ModelMapper modelMapper = new 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; 
    } 
} 

und verwenden Sie es für Ihre Bedürfnisse:

List<PostDTO> listOfPostDTO = ObjectMapperUtils.mapAll(listOfPosts, PostDTO.class); 
+0

Basierend auf Ihren Code, den ich schrieb etwas ähnliches, aber für mich leichter zu verstehen. Und am wichtigsten, "meins". Ich bin generell neu in 'Generics' in' Java'. Vielen Dank für Ihre Hilfe. – Gromo

+0

@Gromo, Sie sind willkommen –

Verwandte Themen