Dozer 使用 xml文件配置1

集合到数组的映射

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

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

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

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

hintList hintList org.dozer.vo.TheFirstSubClassPrime

           

       

在进行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 {

 

  private Set users;

 

  public Set getUsers() {

    return users;

  }

 

  public void setUsers(Set aUsers) {

    users = aUsers;

  }

 

}

public class UserGroupPrime {

 

  private List users;

 

  public List getUsers() {

    return users;

  }

 

  public void setUsers(List aUsers) {

    users = aUsers;

  }

 

}

           

       

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

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

          

  arrayForLists

  listForArray

   

           

       

以下的例子会把 Integer []arrayForLists 转换为ListlistForArray          

  arrayForLists

  listForArray

  java.lang.String 注意看这段 -->

   

           

       

Primitive Array to Primitive Array (bi-directional)

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

          

  anArray

  theMappedArray

     

           

       

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

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

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

global configuration level....

          

 

     non-cumulative

 

           

class mapping level....

          

 

    org.dozer.vo.TestObject

    org.dozer.vo.TestObjectPrime

   

      someList

      someList

       

 

           

       

field mapping level....

          

  hintList

  hintList

  org.dozer.vo.TheFirstSubClass

  org.dozer.vo.TheFirstSubClassPrime

   

 

  unequalNamedList

  theMappedUnequallyNamedList

              

       

Removing Orphans

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

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

          

  srcList

  destList 

   

           

       

 

 

你可能感兴趣的:(Java)