【问题标题】:ModelMapper: Map collections to collections of other structureModelMapper:将集合映射到其他结构的集合
【发布时间】:2015-08-09 17:59:41
【问题描述】:

我将业务对象映射到实体,但在某些情况下,实体的结构与业务对象不同。
我有userCategories,它们以字符串形式存储在业务对象RecipeBo 中,因为BO 不必了解实体的内部结构。这些字符串需要映射到RecipeRecipeUserCategoryRel的关系,除此之外,RecipeBo的另一个字段userId也需要映射到这个RecipeUserCategoryRel中。

我的方法(可行)是创建一个包装器并手动手动创建关系,但这看起来像是在修修补补:

public class BoMapper
{
    private final static ModelMapper modelMapper = new ModelMapper();

    static
    {
        modelMapper.addMappings(new IngredientMap());
    }

    public static void map(Object from, Object to)
    {
        modelMapper.map(from, to);

        if (from instanceof RecipeBo && to instanceof Recipe)
        {
            RecipeBo recipeBo = (RecipeBo)from;
            List<String> userCategories = recipeBo.getUserCategories();
            List<RecipeUserCategoryRel> recipeUserCategoryRels = new ArrayList<>();

            for (String userCategory : userCategories)
            {
                recipeUserCategoryRels.add(new RecipeUserCategoryRel(userCategory, recipeBo.getUserId()));
            }

            Recipe recipe = (Recipe)to;

            recipe.setRecipeUserCategoryRels(recipeUserCategoryRels);
        }
    }
}

我在 BoMapper 中所做的事情是否有更好的方法,例如使用转换器什么的?困难在于映射列表的每个元素并添加userId 字段。

【问题讨论】:

    标签: java mapping modelmapper


    【解决方案1】:

    问题

    这是一个复杂的情况,因为您从其他层次结构而不是直接从列表中获取 userId。 ModelMapper 会将 List 映射到 List 但如果您不将 ModelMapper 配置为 LOOSE,它将无法工作。

    modelMapper.getConfiguration()
      .setMatchingStrategy(MatchingStrategies.LOOSE);
    

    无论如何,如果您以这种方式配置 ModelMapper(松散模式),它将映射 List 并放入您的 Class RecipeUserCategoryRel 的 String 属性(在这种情况下,例如 userCategory 如果它是 String 并且考虑 userId 不是 String ) 其他人(我不太确定)我认为它会为空。

    解决方案

    好吧,我认为解决您的问题的方法是创建一个 Converter 并将其添加到您的 ModelMapper 实例中:

    • RecipeBO(源)-> 食谱(目的地)

    代码如下:

    ModelMapper mapper = new ModelMapper();
    
    Converter<RecipeBO, Recipe> converter = new Converter<RecipeBO,
     Recipe>() {
    
            @Override
            public Recipe convert(MappingContext<RecipeBO, Recipe> context) {
                RecipeBO source = context.getSource();
                Recipe destination = new Recipe();
    
                List<String> userCategoryValues = source.getUserCategories();
                List<RecipeUserCategoryRel> userCategoryToMap = new ArrayList<RecipeUserCategoryRel>();
    
                for(final String userCategory : userCategoryValues){
                    userCategoryToMap.add(new RecipeUserCategoryRel(userCategory,source.getUserId()));
                }
                destination.setRecipeUserCategoryRels(userCategoryToMap);
    
                //... Map other properties if you need
    
                return  destination;
    
            }
        };
    
        //Option 1
        mapper.createTypeMap(RecipeBO.class, Recipe.class).setConverter(converter);
    
        //If you add as a converter directly also works (I don't know which one is better, 
        //choose above option (createTypeMap + setConverter) or the next (addConverter)
        //Option 2 -> mapper.addConverter(converter);
    

    我已经测试过了,它有效!!

    如果我有下一个食谱:

    RecipeBO recipe = new RecipeBO();
    recipe.setUserId("1");
    String values[] = new String[] { "abc", "klm", "xyz", "pqr" };
    
    List<String> list = Arrays.asList(values);
    recipe.setUserCategories(list);
    

    还有一个食谱:

     Recipe recipe = new Recipe();
     List<RecipeUserCategoryRel> recipes = new ArrayList<>();
     recipes.add(new RecipeUserCategoryRel("abc", "1"));        
     recipes.add(new RecipeUserCategoryRel("klm", "1"));
     recipes.add(new RecipeUserCategoryRel("xyz", "1"));
     recipes.add(new RecipeUserCategoryRel("pqr", "1"));
     recipe.setRecipeUserCategoryRels(recipes);
    

    当我将RecipeBO 映射到Recipe 时:

    Recipe actual = mapper.map(getRecipeBO(), Recipe.class);
    

    我得到下一个输出:

    输出:

    - RecipeUserCategoryRel(userCategory=abc, userId=1)
    - RecipeUserCategoryRel(userCategory=klm, userId=1)
    - RecipeUserCategoryRel(userCategory=xyz, userId=1)
    - RecipeUserCategoryRel(userCategory=pqr, userId=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2015-10-23
      • 2021-12-09
      • 1970-01-01
      相关资源
      最近更新 更多