推土机映射中的常量

发布于 2024-07-20 05:45:52 字数 49 浏览 10 评论 0原文

有谁知道如何使用推土机将常量值放入属性中? 我在推土机的文档中没有看到任何相关内容

does anybody know how to put a constant value into an attribute with dozer? I haven't seen anything about that in the dozer's documentation

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

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

发布评论

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

评论(5

多像笑话 2024-07-27 05:45:52

不完全确定你的意思 - 如果你希望 Dozer 在从 BeanA 映射到 BeanB 时总是用常量填充 BeanB.someField ?

您可能需要为此映射注册一个自定义转换器

Not entirely sure what you mean - if you want Dozer to always populate BeanB.someField with a constant whenever you map from BeanA to BeanB?

You might want to register a custom converter for this mapping.

凉栀 2024-07-27 05:45:52

如何利用事件机制?

因此,您可以注册一个侦听器,该侦听器将在侦听器的 mappingFinished() 中设置值。
有关详细信息,请参阅有关事件的推土机文档。 当然,您必须使用某种 if ... instanceof 条件来保护设置代码。

How about taking advantage of the events mechanism?

So you could register a listener which would do the setting of the value in mappingFinished() of your listener.
Consult dozer doc on events for details. Of course you would have to guard the setting code with some kind of if ... instanceof condition.

倾城花音 2024-07-27 05:45:52

最近构建的推土机使这变得更容易。 您可以指定自定义转换器并且您可以为给定字段映射指定该转换器的参数。 创建一个单一的“ConstantConverter”应该很简单,它会接受一个输入参数并在 100% 的情况下将其放入输出字段。

Recent builds of dozer make this easier. You can specify both custom converters and you can specify parameters to that converter for a given field mapping. It should be trivial to create a single 'ConstantConverter' that will take an input parameter and put that in the output field 100% of the time.

久而酒知 2024-07-27 05:45:52

一种可能的实现是:

public class ConstantsCustomConvertor implements ConfigurableCustomConverter{

    private String pararamter;
    @Override
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {     
        return pararamter;
    }

    @Override
    public void setParameter(String parameter) {
        this.pararamter = parameter;

    }
}

示例:

<field custom-converter-param="CONTANT_VALUE" custom-converter="org.yourcompany.ConstantsCustomConvertor">
    <a>a-class-dummyfieldname</a>
    <b>b-class-fieldname</b>
</field>    

此自定义转换器假定 b-class-fieldname 的类型为 String。

One possible implementation is:

public class ConstantsCustomConvertor implements ConfigurableCustomConverter{

    private String pararamter;
    @Override
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {     
        return pararamter;
    }

    @Override
    public void setParameter(String parameter) {
        this.pararamter = parameter;

    }
}

Example:

<field custom-converter-param="CONTANT_VALUE" custom-converter="org.yourcompany.ConstantsCustomConvertor">
    <a>a-class-dummyfieldname</a>
    <b>b-class-fieldname</b>
</field>    

This Custom Convertor is assumes that the b-class-fieldname is of type String.

昔日梦未散 2024-07-27 05:45:52

假设您只想在单向映射中执行此操作,则以下内容适用于字符串常量:

/**
 * Custom one-way Dozer converter mapping to constant string value specified by a parameter.
 */
public class OneWayStringConstantConverter extends DozerConverter<Class, String> {

    public OneWayBooleanConstantConverter() {
        super(Class.class, String.class);
    }

    @Override
    public String convertTo(Class aClass, String aString) {
        // Return constant value specified by the parameter (source is ignored)
        return getParameter();
    }

    @Override
    public Class convertFrom(String aString, Class aClass) {
        throw new UnsupportedOperationException(
                "OneWayStringConstantConverter should only be used in one-way mappings");
    }
}

...调用者:

  <field custom-converter="full.path.to.OneWayStringConstantConverter"
         custom-converter-param="My constant string value">
     <a>class</a> <!-- Source not used: Converter only sets target of a one-way mapping -->
     <b>targetField</b>
  </field>

建议使用 class 值作为虚拟“源字段”,因为尚未使用 getClass() 方法保证存在于任何对象上。

我需要为布尔值执行此操作,所以使用了这个:

/**
 * Custom one-way Dozer converter which maps to the constant boolean value (true or false)
 * specified by a parameter.
 */
public class OneWayBooleanConstantConverter extends DozerConverter<Class, Boolean> {

    public OneWayBooleanConstantConverter() {
        super(Class.class, Boolean.class);
    }

    @Override
    public Boolean convertTo(Class aClass, Boolean aBoolean) {
        // Return constant boolean value specified by the parameter (source is ignored)
        return Boolean.parseBoolean(getParameter());
    }

    @Override
    public Class convertFrom(Boolean aBoolean, Class aClass) {
        throw new UnsupportedOperationException(
                "OneWayBooleanConstantConverter should only be used in one-way mappings");
    }
}

Assuming you only want to do this in a one-way mapping, the following will work for a String constant:

/**
 * Custom one-way Dozer converter mapping to constant string value specified by a parameter.
 */
public class OneWayStringConstantConverter extends DozerConverter<Class, String> {

    public OneWayBooleanConstantConverter() {
        super(Class.class, String.class);
    }

    @Override
    public String convertTo(Class aClass, String aString) {
        // Return constant value specified by the parameter (source is ignored)
        return getParameter();
    }

    @Override
    public Class convertFrom(String aString, Class aClass) {
        throw new UnsupportedOperationException(
                "OneWayStringConstantConverter should only be used in one-way mappings");
    }
}

... invoked by:

  <field custom-converter="full.path.to.OneWayStringConstantConverter"
         custom-converter-param="My constant string value">
     <a>class</a> <!-- Source not used: Converter only sets target of a one-way mapping -->
     <b>targetField</b>
  </field>

Would recommend using the class value as a dummy "source field" since it is not used yet getClass() method is guaranteed to exist on any object.

I needed to do it for a Boolean instead so used this:

/**
 * Custom one-way Dozer converter which maps to the constant boolean value (true or false)
 * specified by a parameter.
 */
public class OneWayBooleanConstantConverter extends DozerConverter<Class, Boolean> {

    public OneWayBooleanConstantConverter() {
        super(Class.class, Boolean.class);
    }

    @Override
    public Boolean convertTo(Class aClass, Boolean aBoolean) {
        // Return constant boolean value specified by the parameter (source is ignored)
        return Boolean.parseBoolean(getParameter());
    }

    @Override
    public Class convertFrom(Boolean aBoolean, Class aClass) {
        throw new UnsupportedOperationException(
                "OneWayBooleanConstantConverter should only be used in one-way mappings");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文