zoukankan      html  css  js  c++  java
  • 使用dozer将DTO转化为DO

    DTO,就是Data Transfer Object,数据传输对象,可以简单理解成请求中的对象。
    PO,就是Persistant Object,持久化对象,它跟持久层(通常是关系型数据库)的数据结构形成一一对应的映射关系,如果持久层是关系型数据库,那么,数据表中的每个字段(或若干个)就对应PO的一个(或若干个)属性。
    DO,领域对象(Domain Object),就是从现实世界中抽象出来的有形或无形的业务实体。

    比如,在请求接口中,请求参数使用的对象OrderDTO包括订单id,商品名称,商品详情,以及密钥、企业id等其他相关的信息,
    而在插入订单表的时候,订单表对应的订单对象Order由订单id,商品名称,商品详情构成,不需要其他的冗余信息,
    那么需要将OrderDTO对象转换为Order对象。
    在开发中,需要将DTO转化为DO时,可以使用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和DO中相同含义的属性, 使用的是相同名称的变量,所以在工具类内部是可以直接转换的,
    比如name,age,score。这几个在DTO和DO中,变量名都是相同的。
    如果变量名是不同的,那么需要另外在xml或者类中进行配置。
    这个后面再拓展。

    参考资料:
    https://juejin.im/post/5d5e021c6fb9a06af13d6f67

  • 相关阅读:
    预备作业03 20162311张之睿
    [LeetCode 题解]: String to Interger (atoi)
    [LeetCode 题解]: Add Two Numbers
    [LeetCode 题解]: Interger to Roman
    [LeetCode 题解]: Longest Substring Without Repeating Characters
    [LeetCode 题解]: Roman to Interger
    [LeetCode 题解]: palindromes
    [LeetCode 题解]: Two Sum
    [LeetCode 题解]: Maximum Subarray
    [LeetCode 题解]:Gas Station
  • 原文地址:https://www.cnblogs.com/expiator/p/11614599.html
Copyright © 2011-2022 走看看