DTO,就是Data Transfer Object,数据传输对象。
在开发中,经常需要将DTO转化为PO,可以使用dozer。
Maven
<dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency>
Utils
工具类BeanMapperUtils 。
import java.util.Collection; import java.util.List; import com.google.common.collect.Lists; import org.dozer.DozerBeanMapper; public class BeanMapperUtils { /** * 持有Dozer单例, 避免重复创建DozerMapper消耗资源. */ private static DozerBeanMapper dozer = new DozerBeanMapper(); /** * 基于Dozer转换对象的类型. */ public static <T> T map(Object source, Class<T> destinationClass) { return dozer.map(source, destinationClass); } /** * 基于Dozer转换Collection中对象的类型. */ public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) { List<T> destinationList = Lists.newArrayList(); for (Object sourceObject : sourceList) { T destinationObject = dozer.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; } /** * 基于Dozer将对象A的值拷贝到对象B中. */ public static void copy(Object source, Object destinationObject) { dozer.map(source, destinationObject); } }
对象类
User.java如下:
注意,对象类一定要有一个无参数的构造方法。否则在转换对象时,会报错"java.lang.NoSuchMethodException"
public class User { private String name; private int age; private BigDecimal score; public User(String name, int age, BigDecimal score) { this.name = name; this.age = age; this.score = score; } //注意,这个构造方法必须加上。 public User() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BigDecimal getScore() { return score; } public void setScore(BigDecimal score) { this.score = score; } }
UserDTO.java,如下所示:
public class UserDTO { private String name; private String adress; private int age; private BigDecimal score; private String description; public UserDTO(String name, String adress, int age, BigDecimal score, String description) { this.name = name; this.adress = adress; this.age = age; this.score = score; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAdress() { return adress; } public void setAdress(String adress) { this.adress = adress; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BigDecimal getScore() { return score; } public void setScore(BigDecimal score) { this.score = score; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
运用示例:
public class DTOTest { public static void main(String[] args) { UserDTO userDTO=new UserDTO("lin", "shenZhen", 26, BigDecimal.ONE, "test"); User user=BeanMapperUtils.map(userDTO,User.class); System.out.println(user.getName()+","+user.getAge()+","+user.getScore()); } }
其他
以上举的例子中,DTO和PO中相同含义的属性, 使用的是相同名称的变量,所以在工具类内部是可以直接转换的,
比如name,age,score。这几个在DTO和PO中,变量名都是相同的。
如果变量名是不同的,那么需要另外在xml或者类中进行配置。
这个后面再拓展。