Dozer 使用 xml文件配置1

来源:互联网 发布:淘宝活动流程 编辑:程序博客网 时间:2024/06/05 19:35

集合到数组的映射

以下的集合会Dozer会自动处理

  • List to List
  • List to Array
  • Array to Array
  • Set to Set
  • Set to Array
  • Set to List

 使用建议的集合映射关系:

Dozer会聪明的自己去对集合进行类之间的复制,但还是建议在xml文件中明确的写出来          

           

       

在进行Array Set List之间转换的时候,多少还是会发生一些故障,不管你是否明确的写出了映射:

  • List to List
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type
  • Array to List
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type
  • List to Array
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest array will contain data types defined by the array
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
  • Array to Array
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest array will contain data types defined by the array
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
  • Set to Set
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type
  • Array to Set
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type
  • Set to Array
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest array will contain data types defined by the array
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
  • List to Set
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type
  • Set to List
    • Dest Hint req'd: NO
    • Dest Hint allowed: YES
    • If no dest hint specified: Dest list will contain the same data types in the source
    • If hint is speficied: Dest list will contain objects that match dest hint(s) type

Using JDK 1.5 Generics for Collection Mapping

JDK 15 的泛型映射

如果你使用的是jdk1.5以上的话,dozer会自动的识别参数化过得映射,比如

          

public class UserGroup {

 

  privateSet<User> users;

 

  publicSet<User> getUsers() {

    return users;

  }

 

  public voidsetUsers(Set<User> aUsers) {

    users = aUsers;

  }

 

}

public class UserGroupPrime {

 

  privateList<UserPrime> users;

 

  publicList<UserPrime> getUsers() {

    return users;

  }

 

  public voidsetUsers(List<UserPrime> aUsers) {

    users = aUsers;

  }

 

}

           

       

 Object类型的数组to List(双向的)

当执行此类转换的时候,需要在List中指定具体的类型,即用List<>方式构造

          

<field>

 <a>arrayForLists</a><!-- changing an Integer [] to Listand back again -->

 <b>listForArray</b>

</field>   

           

       

以下的例子会把 Integer []arrayForLists转换为List<String>listForArray          

<field>

  <a>arrayForLists</a><!--changing an Integer [] to List and back again -->

 <b>listForArray</b>

 <b-hint>java.lang.String</b-hint> <!—注意看这段 -->

</field>   

           

       

Primitive Array to Primitive Array (bi-directional)

基本类型的数组to基本类型的数组

          

<field>

 <a>anArray</a> <!-- converting int[] to int [] by nameonly -->

 <b>theMappedArray</b>

</field>     

           

       

Cumulative vs. Non-Cumulative List Mapping (bi-directional)

If you are mapping to a Class which has already been initialized, dozerwill either 'add' or 'update' objects to your List. If your List or Set alreadyhas objects in it dozer checks the mapped List, Set, or Array and calls thecontains() method to determine if it needs to 'add' or 'update'. This isdetermined using the relationship-type attribute on the field tag. The defaultis 'cumulative'. relationship-type can be specifed at the field mapping, classmapping, or global configuration level.

Dozer非常的聪明,他还支持将一个集合的内容添加或者更新到另外一个集合当中,只是需要在xml文件当中指定relationship-type的值(non-cumulative| cumulative) dozer是根据contains()这个方法去判断的,如果使用了relationship-type这个属性,那么默认就是'cumulative'

global configuration level....

          

<mappings>

 <configuration>

    <relationship-type>non-cumulative</relationship-type>

 </configuration>

</mappings>

           

class mapping level....

          

<mappings>

  <mappingrelationship-type="non-cumulative">

   <class-a>org.dozer.vo.TestObject</class-a>

   <class-b>org.dozer.vo.TestObjectPrime</class-b>

    <field>

     <a>someList</a>

      <b>someList</b>

   </field>   

  </mapping>

</mappings>           

       

field mapping level....

          

<field relationship-type="cumulative">

 <a>hintList</a> <!-- objects will always be added to anexisting List -->

 <b>hintList</b>

  <a-hint>org.dozer.vo.TheFirstSubClass</a-hint>

 <b-hint>org.dozer.vo.TheFirstSubClassPrime</b-hint>

</field>   

 

<fieldrelationship-type="non-cumulative">

 <a>unequalNamedList</a> <!-- objects will updated ifalready exist in List, added if they are not present -->

 <b>theMappedUnequallyNamedList</b>

</field>              

       

Removing Orphans

Orphans are elements which exist in a destination collection that did notexist within the source collection. Dozer will remove orphans by calling the'remove' method on actual orphans of the underlying destination collection; itwill not clear all. To determine elements which are orphans dozer uses thecontains() method to check if the results contains orphans. The default isfalse.

移除孤立的字段的配置..(英文这么多,我写的就这么多,精辟啊:) )

          

<field remove-orphans="true">

 <a>srcList</a>

 <b>destList</b> <!-- orphan objects will always be removed from an existingdestination List -->

</field>   

           

       

 

 

原创粉丝点击