ModelMapper:将集合映射到其他结构

时间:2015-08-09 17:59:41

标签: java mapping modelmapper

我将业务对象映射到实体,并且存在实体的结构与业务对象不同的情况 我有userCategories作为字符串存储在业务对象RecipeBo中,因为BO不必知道有关实体内部结构的任何信息。这些字符串需要映射到RecipeRecipeUserCategoryRel的关系,除此之外,还需要在{{1}中映射userId的另一个字段RecipeBo也是。

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

RecipeUserCategoryRel

我是否有更好的方法来处理我在BoMapper中所做的事情,例如:使用转换器还是什么?难点在于映射列表的每个元素并添加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); } } } 字段。

1 个答案:

答案 0 :(得分:2)

ISSUE

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

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

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

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

  • RecipeBO(来源) - &gt;食谱(目的地)

代码如下:

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);

和RecipeBO:

 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 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)