推土机自定义转换器ID映射:通过DozerConverter getParameter将对象转换为长对象

Mat*_* B. 5 mapping spring converter gwt-rpc dozer

我需要帮助配置我的推土机映射文件.

主要是我想知道如何让用户用户obejct转换为Long userId.
因此map:user >> userId
但是我有多个对象,例如注释>> commentId或address >> addressId

因此,我想要比为每个领域编写映射更优雅.所有对象都实现了Loadable接口.

由于getParameter()DozerConverter方法,波纹管代码现在正在运行,但是如果你知道比我写的转换器更好的方法,请告诉我.

// dozer.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<configuration>
<custom-converters>
  <converter type="project.shared.domain.dto.dozer.LoadableIdConverter" >
    <class-a>project.shared.domain.Loadable</class-a>
    <class-b>java.lang.Long</class-b>
  </converter>
</custom-converters>
</configuration>

<mapping>
    <class-a>project.shared.domain.Suggestion</class-a>
    <class-b>project.shared.domain.dto.DTOSuggestion</class-b>
    <field custom-converter-param="User">
        <a>user</a>
        <b>userId</b>
    </field>
</mapping>

</mappings>\
Run Code Online (Sandbox Code Playgroud)

// Spring Application上下文

<bean id="loadableIdConverter" class="project.shared.domain.dto.dozer.LoadableIdConverter">
    <property name="userService" ref="userService"/>
    <property name="commentService" ref="commentService"/>
    <property name="addressService" ref="addressService"/>
</bean>
<bean id="gwtMapper" class="org.dozer.DozerBeanMapper">
    <property name="mappingFiles">
        <list>
            <value>classpath:/dozer.xml</value>
        </list>
    </property>

    <property name="customConverters">
        <list>
            <ref bean="loadableIdConverter"/>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

//标准的hibernate对象

public class Suggestion implements Serializable, Loadable {
    private long id = -1;
    private Date dateCreated;

    private User user; //trying to use dozer to covert this bad boy to Long userId
    //...
} 
Run Code Online (Sandbox Code Playgroud)

// DTO对象

public class DTOSuggestion implements IsSerializable {
   private long id = -1;
   private Date dateCreated;

   private Long userId; //trying to get this ID via the dozer converter
   //...
}
Run Code Online (Sandbox Code Playgroud)

//可加载的界面

public interface Loadable extends Serializable {
    public long getId();
    public void setId(long id);
}
Run Code Online (Sandbox Code Playgroud)

//推土机转换器

public class LoadableIdConverter extends DozerConverter<Loadable, Long> {

    private UserService userService; //configured in applicationcontext
    private AddressService addressService; //configured in applicationcontext
    private CommentService commentService; //configured in applicationcontext 

    public LoadableIdConverter() {
        super(Loadable.class, Long.class);
    }

    public Long convertTo(Loadable object, Long id) {
        return object.getId();
    }

    public Loadable convertFrom(Long id, Loadable object) {
        if (id < 0) return null;

        String loadable = getParameter();
        if (loadable.equalsIgnoreCase("User"))
            return userService.get(User.class, id);
        if (loadable.equalsIgnoreCase("Address"))
            return addressService.get(Address.class, id);
        if (loadable.equalsIgnoreCase("Comment"))
            return commentService.get(Comment.class, id);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*din 4

您可以使用一种技巧来避免转换器参数。如果您回退到 Dozer 中较旧的自定义转换器方法(实现 CustomConverter 接口),您将获得两个附加参数:existingDestinationValue 和destinationClass。

convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) 
Run Code Online (Sandbox Code Playgroud)

通过使用这些值,您可以通过反射内省您的目标字段,并了解 Loadable 接口的预期具体实现是什么。当然,只有当您使用具体类型定义字段类型时,这才有效。但是您的示例中已经有了它,所以这应该不是问题。CustomConverter 实现将更加冗长,因为您需要手动确定映射的方向,但它使您可以完全控制映射过程中发生的情况。