Dozer 自定义转换器 ID 映射:通过 DozerConverter getParameter 对象到 Long 以及 Long 到对象

发布于 2024-10-07 03:38:38 字数 3689 浏览 3 评论 0原文

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

主要是想知道如何让User用户对象转换为Long userId。
因此地图:用户>>用户ID
但我有多个对象,例如评论>>评论ID或地址>>因此

,我想要比仅仅为每个字段编写映射更优雅的东西。所有对象都实现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>\

// Spring应用程序上下文

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

//标准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
    //...
} 

//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
   //...
}

//可加载接口

public interface Loadable extends Serializable {
    public long getId();
    public void setId(long id);
}

//Dozer转换器

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

I need help configuring my dozer mapping file.

Mainly I would like to know how to get User user obejct to convert to Long userId.
Hence map: user >> userId
But I have multiple objects such as comment >> commentId or address >> addressId

therefor I'd like to have something more elegant than just writing mapping for each of the fields. All of the object implement Loadable interface.

The bellow code is now functioning thanks to the getParameter() DozerConverter method, but if you know any better way than the converter that I wrote please let me know.

// 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>\

// Spring Application context

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

//Standard hibernate object

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
    //...
} 

//DTO object

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

   private Long userId; //trying to get this ID via the dozer converter
   //...
}

//Loadable interface

public interface Loadable extends Serializable {
    public long getId();
    public void setId(long id);
}

//Dozer converter

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-10-14 03:38:38

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

convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) 

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

There is one trick you could use to avoid converter parameters. If you fall back to older custom converter approach in Dozer, which is implementing CustomConverter interface, you will get two additional parameters: existingDestinationValue and destinationClass.

convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) 

By using these values you could introspect your destination field via reflection and know what is the expected concrete implementation of Loadable interface. This works only if you define the field types with concrete types of course. But you already have it in your example, so this should not be a problem. CustomConverter implementation will be more verbose as you need to determine the direction of the mapping manually, but it gives you full control of what is going on during the mapping process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文