Copying one class's fields into another class's identical fields

后端 未结 4 1276
耶瑟儿~
耶瑟儿~ 2021-01-02 04:23

I have this question. But it will be difficult for me to explain as I don\'t know exact terms to use. Hope someone will understand. I\'ll try to discribe to the best i can.

4条回答
  •  再見小時候
    2021-01-02 04:57

    Did you ever heared about Dozer ? : http://dozer.sourceforge.net/

    Dozer

    Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

    Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

    Dozer allow you to map Java Beans :

    • using their names (implicit mapping), i.e mapping ClassA.x to ClassB.x
    • providing hability to map diffrent structures (explicit mapping) with different names with (quite simple) xml or annoation configuration .

    Here a XML example on the library site :

    
    
              
       
        org.dozer.vo.TestObject
        org.dozer.vo.TestObjectPrime   
        
          one
          onePrime
        
        
    
          
    
    
    

    This will map object org.dozer.vo.TestObject into TestObjectPrime, mapping all variable that are identicals together (like in your case) and variables TestObjectFoo.oneFoo into TestObjectFooPrime.oneFooPrime.

    Great, isn't it ?

提交回复
热议问题