国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
復(fù)制bean的開(kāi)源工具Dozer

項(xiàng)目中,經(jīng)常會(huì)遇到各層對(duì)象之間相互進(jìn)行值傳遞的過(guò)程,如在數(shù)據(jù)據(jù)持久層有一持久類(lèi)ClassA,在視圖層可能會(huì)變?yōu)閂iewA,通常情況下,如果兩個(gè)類(lèi)結(jié)構(gòu)一樣,最常使用的是BeanUtils.copyProperties(src,aim)方法將一個(gè)對(duì)象的值賦給另一個(gè)對(duì)象!但是如果屬性不同或者名稱(chēng)不同,則需要Dozer來(lái)完成,通過(guò)靈活的配置,達(dá)到不同對(duì)象間的拷貝!dozer當(dāng)字段名稱(chēng)相同時(shí),可以直接復(fù)制,不相同時(shí),可通過(guò)配置文件進(jìn)行復(fù)制

 

具體看一個(gè)比較復(fù)雜的例子:

 

Java代碼  
  1. package com.jasson.mode;  
  2.   
  3. public enum GenderType {  
  4.     male,   //男     
  5.     female  //女  
  6. }  

 

Java代碼  
  1. package com.jasson.mode;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  * @author jgao1 
  7.  *  
  8.  */  
  9. public class Info {  
  10.     private int id;  
  11.     private Date createDate;  
  12.     private GenderType gender;  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(int id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     public Date getCreateDate() {  
  23.         return createDate;  
  24.     }  
  25.   
  26.     public void setCreateDate(Date createDate) {  
  27.         this.createDate = createDate;  
  28.     }  
  29.   
  30.     public GenderType getGender() {  
  31.         return gender;  
  32.     }  
  33.   
  34.     public void setGender(GenderType gender) {  
  35.         this.gender = gender;  
  36.     }  
  37. }  

 

Java代碼  
  1. package com.jasson.mode;  
  2.   
  3. public class InfoVO {  
  4.     private int id;  
  5.     private String date;  
  6.     private Integer gender;  
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getDate() {  
  14.         return date;  
  15.     }  
  16.     public void setDate(String date) {  
  17.         this.date = date;  
  18.     }  
  19.     public Integer getGender() {  
  20.         return gender;  
  21.     }  
  22.     public void setGender(Integer gender) {  
  23.         this.gender = gender;  
  24.     }  
  25. }  

 

Java代碼  
  1. package com.jasson.mode;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String name;  
  6.     private String password;  
  7.     private Info info;  
  8.     private String option;  
  9.   
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.   
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.   
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     public String getPassword() {  
  26.         return password;  
  27.     }  
  28.   
  29.     public void setPassword(String password) {  
  30.         this.password = password;  
  31.     }  
  32.   
  33.     public Info getInfo() {  
  34.         return info;  
  35.     }  
  36.   
  37.     public void setInfo(Info info) {  
  38.         this.info = info;  
  39.     }  
  40.       
  41.     public String getOption() {  
  42.         return option;  
  43.     }  
  44.   
  45.     public void setOption(String option) {  
  46.         this.option = option;  
  47.     }  
  48. }  

 

Java代碼  
  1. package com.jasson.mode;  
  2.   
  3. public class UserVO {  
  4.     private int id;  
  5.     private String userName;  
  6.     private String password;  
  7.     private InfoVO info;  
  8.     private Integer option;  
  9.   
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.   
  14.     public void setId(int id) {  
  15.         this.id = id;  
  16.     }  
  17.   
  18.     public String getUserName() {  
  19.         return userName;  
  20.     }  
  21.   
  22.     public void setUserName(String userName) {  
  23.         this.userName = userName;  
  24.     }  
  25.   
  26.     public String getPassword() {  
  27.         return password;  
  28.     }  
  29.   
  30.     public void setPassword(String password) {  
  31.         this.password = password;  
  32.     }  
  33.   
  34.     public InfoVO getInfo() {  
  35.         return info;  
  36.     }  
  37.   
  38.     public void setInfo(InfoVO info) {  
  39.         this.info = info;  
  40.     }  
  41.   
  42.     public Integer getOption() {  
  43.         return option;  
  44.     }  
  45.   
  46.     public void setOption(Integer option) {  
  47.         this.option = option;  
  48.     }  
  49. }  

 兩個(gè)對(duì)象相互轉(zhuǎn)換,需要如下自定義convert

Java代碼  
  1. package com.jasson.util;  
  2.   
  3. import org.dozer.CustomConverter;  
  4.   
  5. import com.jasson.mode.GenderType;  
  6.   
  7. public class EnumConverter implements CustomConverter {  
  8.     public Object convert(Object destinationFieldValue,  
  9.             Object sourceFieldValue, Class<?> destinationClass,  
  10.             Class<?> sourceClass) {  
  11.         Object returnVale = null;  
  12.         if (sourceFieldValue != null) {  
  13.             if (sourceFieldValue instanceof Integer) {  
  14.                 if ((Integer) sourceFieldValue == 0) {  
  15.                     returnVale = GenderType.male;  
  16.                 } else if ((Integer) sourceFieldValue == 1) {  
  17.                     returnVale = GenderType.female;  
  18.                 }  
  19.             } else if (sourceFieldValue instanceof Enum) {  
  20.                 if (sourceFieldValue == GenderType.male) {  
  21.                     returnVale = 0;  
  22.                 } else if (sourceFieldValue == GenderType.female) {  
  23.                     returnVale = 1;  
  24.                 }  
  25.             }  
  26.         }  
  27.         return returnVale;  
  28.     }  
  29. }  

 

Java代碼  
  1. package com.jasson.util;  
  2.   
  3. import org.dozer.CustomConverter;  
  4.   
  5. public class SpecialConverter implements CustomConverter {  
  6.     public Object convert(Object destinationFieldValue,  
  7.             Object sourceFieldValue, Class<?> destinationClass,  
  8.             Class<?> sourceClass) {  
  9.         Object returnVale = null;  
  10.         if (sourceFieldValue != null) {  
  11.             if (sourceFieldValue instanceof Integer) {  
  12.                 if ((Integer) sourceFieldValue == 0) {  
  13.                     returnVale = "NO";  
  14.                 } else if ((Integer) sourceFieldValue == 1) {  
  15.                     returnVale = "OK";  
  16.                 }  
  17.             } else if (sourceFieldValue instanceof String) {  
  18.                 if (sourceFieldValue.equals("NO")) {  
  19.                     returnVale = 0;  
  20.                 } else if (sourceFieldValue.equals("OK")) {  
  21.                     returnVale = 1;  
  22.                 }  
  23.             }  
  24.         }  
  25.         return returnVale;  
  26.     }  
  27. }  

 

配置的xml文件如下:

 

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <mappings xmlns="http://dozer.sourceforge.net"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xsi:schemaLocation="http://dozer.sourceforge.net  
  5.       http://dozer.sourceforge.net/schema/beanmapping.xsd">  
  6.         
  7.       <configuration>  
  8.         <stop-on-errors>true</stop-on-errors>  
  9.         <date-format>yyyy-MM-dd</date-format>  
  10.         <wildcard>true</wildcard>  
  11.         <custom-converters>   
  12.             <converter type=" com.jasson.util.EnumConverter">  
  13.                 <class-a>java.lang.Enum</class-a>  
  14.                 <class-b>java.lang.Integer</class-b>  
  15.             </converter>  
  16.         </custom-converters>  
  17.       </configuration>  
  18.   
  19.       <mapping date-format="yyyy-MM-dd">  
  20.         <class-a>com.jasson.mode.User</class-a>  
  21.         <class-b>com.jasson.mode.UserVO</class-b>  
  22.         <field>  
  23.             <a>name</a>  
  24.             <b>userName</b>  
  25.         </field>  
  26.         <field>  
  27.             <a>info.createDate</a>  
  28.             <b>info.date</b>  
  29.         </field>  
  30.         <field custom-converter="com.jasson.util.EnumConverter">  
  31.             <a>info.gender</a>  
  32.             <b>info.gender</b>  
  33.         </field>  
  34.         <field custom-converter="com.jasson.util.SpecialConverter">  
  35.             <a>option</a>  
  36.             <b>option</b>  
  37.         </field>  
  38.       </mapping>  
  39. </mappings>  
  40.   
  41.      

 

spring中的配置如下:

 

 

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd   
  7.     http://www.springframework.org/schema/tx   
  8.     http://www.springframework.org/schema/tx/spring-tx.xsd   
  9.     http://www.springframework.org/schema/jee   
  10.     http://www.springframework.org/schema/jee/spring-jee.xsd   
  11.     http://www.springframework.org/schema/context   
  12.     http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true">  
  13.     <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">  
  14.         <property name="mappingFiles">  
  15.             <list>  
  16.                 <value>classpath*:dozerconfig/dozerBeanMapping.xml</value>  
  17.             </list>  
  18.         </property>  
  19.     </bean>  
  20. </beans>  

 

Java代碼  
  1. package com.jasson.Test;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.dozer.Mapper;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import com.jasson.mode.GenderType;  
  10. import com.jasson.mode.Info;  
  11. import com.jasson.mode.User;  
  12. import com.jasson.mode.UserVO;  
  13.   
  14. public class MainTest {  
  15.   
  16.     /** 
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) {  
  20.         // TODO Auto-generated method stub  
  21.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  22.                 "applicationContext.xml");  
  23.         Mapper mapper = (Mapper) ctx.getBean("mapper");  
  24.         Info info = new Info();  
  25.         info.setCreateDate(new Date());  
  26.         info.setGender(GenderType.female);  
  27.         info.setId(100);  
  28.           
  29.         User user = new User();  
  30.         user.setId(100);  
  31.         user.setName("jasson");  
  32.         user.setPassword("password");  
  33.         user.setInfo(info);  
  34.         user.setOption("NO");  
  35.   
  36.         UserVO uservo = mapper.map(user, UserVO.class);  
  37.         System.out.println(user.getId()+"<------>"+uservo.getId());  
  38.         System.out.println(user.getPassword()+"<------>"+uservo.getPassword());  
  39.         System.out.println(user.getName()+"<------>"+uservo.getUserName());  
  40.         System.out.println(user.getInfo().getId()+"<------>"+uservo.getInfo().getId());  
  41.         System.out.println(user.getInfo().getGender()+"<------>"+uservo.getInfo().getGender());  
  42.         System.out.println(user.getInfo().getCreateDate()+"<------>"+uservo.getInfo().getDate());  
  43.         System.out.println(user.getOption()+"<------>"+uservo.getOption());  
  44.     }  
  45. }  

 

結(jié)果如下:

 

100<------>100
password<------>password
jasson<------>jasson
100<------>100
female<------>1
Mon May 14 18:15:29 CST 2012<------>2012-05-14
NO<------>0

實(shí)現(xiàn)完全的復(fù)制功能。

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
springboot之使用redistemplate優(yōu)雅地操作redis
SSH提示ognl.OgnlException: target is null for setProperty
Spring Data MongoDB 一:入門(mén)篇(環(huán)境搭建、簡(jiǎn)單的CRUD操作)
spring入門(mén)經(jīng)典三
MongoDB整合Spring
Activemq和Lingo實(shí)現(xiàn)JMS和異步調(diào)用-Java頻道
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服