简体   繁体   English

MapStruct:从 java.util.Map 映射到 Bean?

[英]MapStruct: mapping from java.util.Map to Bean?

I currently have a Map<String, String> that contains values in the form key = value and I would like to "expand" those into a real object.我目前有一个Map<String, String> ,其中包含key = value形式的key = value ,我想将它们“扩展”为一个真实的对象。

Is it possible to automate that with MapStruct and how would I do that?是否可以使用 MapStruct 自动执行此操作,我将如何执行此操作?

To clarify: The code I would write by hand would look something like this:澄清一下:我手工编写的代码如下所示:

public MyEntity mapToEntity(final Map<String, String> parameters) {
  final MyEntity result = new MyEntity();
  result.setNote(parameters.get("note"));
  result.setDate(convertStringToDate(parameters.get("date")));
  result.setCustomer(mapIdToCustomer(parameters.get("customerId")));
  // ...
  return result;
}

Method 1 方法1

The MapStruct repo gives us useful examples such as Mapping from map . MapStruct repo为我们提供了有用的示例,例如Mapping from map

Mapping a bean from a java.util.Map would look something like : 从java.util.Map映射bean看起来像:

@Mapper(uses = MappingUtil.class )
public interface SourceTargetMapper {

    SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );

    @Mappings({
        @Mapping(source = "map", target = "ip", qualifiedBy = Ip.class),
        @Mapping(source = "map", target = "server", qualifiedBy = Server.class),
    })
    Target toTarget(Source s);
}

Notice the use of the MappingUtil class to help MapStruct figuring out how to correctly extract values from the Map : 请注意使用MappingUtil类来帮助MapStruct确定如何从Map中正确提取值:

public class MappingUtil {

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Ip {
    }

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public static @interface Server {
    }

    @Ip
    public String ip(Map<String, Object> in) {
        return (String) in.get("ip");
    }

    @Server
    public String server(Map<String, Object> in) {
        return (String) in.get("server");
    }
}

Method 2 方法2

As per Raild comment on the issue related to this post , it is possible to use MapStruct expressions to achieve similar results in a shorter way : 根据Raild对与此帖相关的问题的评论,可以使用MapStruct表达式以更短的方式实现类似的结果:

@Mapping(expression = "java(parameters.get(\"name\"))", target = "name")
public MyEntity mapToEntity(final Map<String, String> parameters);

No note on performance though and type conversion may be trickier this way but for a simple string to string mapping, it does look cleaner. 虽然没有关于性能的说明和类型转换可能比较简单但是对于简单的字符串到字符串映射,它确实看起来更干净。

Since Jul 2021 MapStruct supports mapping from Map to POJO .自 2021 年 7 月以来,MapStruct 支持从 Map 到 POJO 的映射

Example:例子:

@Mapper
public interface CustomerMapper {
    @Mapping(target = "name", source = "customerName")
    Customer toCustomer(Map<String, String> map);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM