How to convert from DAO to DTO object using ModelMapper in java

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2020-10-26 13:02:35 Viewed : 501


How to convert from DAO to DTO object using ModelMapper in java

In Java, Model Mapper is part of org.modelmapper package

1.   model-mapper API.

Using this API, we can avoid manual setter and getters as explained below example

org.modelmapper.ModelMapper

ModelMapper - Performs object mapping, maintains Configuration and stores TypeMaps.

·        To perform object mapping use map.

·        To configure the mapping of one type to another use createTypeMap.

·        To add mappings for specific properties use addMappings supplying a PropertyMap.

·        To configure ModelMapper use getConfiguration.

·        To validate mappings use validate.


 2. Conversion of one entity to another entity without manual setters and getters:

2.1. Add below dependecy in pom.xml

<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->

<dependency>

    <groupId>org.modelmapper</groupId>

    <artifactId>modelmapper</artifactId>

    <version>0.7.5</version>

</dependency>

2.2   The following examples demonstrate usage of ModelMapper for common use cases.   

Save the file as −  ModelMapperEx.java

/* Convert from employeeDO object to employeeDTO without manual setters and getters */

 

ModelMapperEx.java

 

 package runnerdev;

 

import org.modelmapper.ModelMapper;

 

public class ModelMapperEx {

     private static final ModelMapper modelMapper = new ModelMapper();

 

     public static void main(String[] args) {

           EmployeeDO empDO = new EmployeeDO();

           empDO.setEmpID(111);

           empDO.setEmpName("Ram");

           // Convert from employeeDO object to employeeDTO

           EmployeeDTO empDTO = modelMapper.map(empDO, EmployeeDTO.class);

           System.out.println("employeeId: " + empDTO.getEmpID());

           System.out.println("employeeName: " + empDTO.getEmpName());

     }

 

}

 

class EmployeeDO {

 

     private int empID;

 

     public int getEmpID() {

           return empID;

     }

 

     public void setEmpID(int empID) {

           this.empID = empID;

     }

 

     public String getEmpName() {

           return empName;

     }

 

     public void setEmpName(String empName) {

           this.empName = empName;

     }

 

     private String empName;

 

     @Override

     public String toString() {

           return "EmployeeDO [empID=" + empID + ", empName=" + empName + "]";

     }

}

 

class EmployeeDTO {

 

     private String empID;

 

     public String getEmpID() {

           return empID;

     }

 

     public void setEmpID(String empID) {

           this.empID = empID;

     }

 

     public String getEmpName() {

           return empName;

     }

 

     public void setEmpName(String empName) {

           this.empName = empName;

     }

 

     private String empName;

 

     @Override

     public String toString() {

           return "EmployeeDTO [empID=" + empID + ", empName=" + empName + "]";

     }

}

                                                  

  

Output:

employeeId: 111

employeeName: Ram


3. Conversion of List DO to DTO with out manual setters and getters

 ModelMapperListEx.java

 package runnerdev;

 

import java.util.ArrayList;

import java.util.List;

 

import org.modelmapper.ModelMapper;

 

public class ModelMapperListEx {

     private static final org.modelmapper.ModelMapper modelMapper = new ModelMapper();

 

     public static void main(String[] args) {

           List<EmployeeDo> empDOList = new ArrayList<>();

           EmployeeDo em = new EmployeeDo();

           em.setEmpId(22);

           em.setEmpName("Prasad");

           EmployeeDo em1 = new EmployeeDo();

           em1.setEmpId(32);

           em1.setEmpName("Bonam");

           empDOList.add(em);

           empDOList.add(em1);

           empDOList.forEach(p -> {

                System.out.println(p);

           });

 

           // Convert from empDOList object to employeeDTOList

 

           List<EmployeeDTO> empDTOList = new ArrayList<>();

           empDOList.forEach((emp) -> {

              EmployeeDTO empDTO = modelMapper.map(emp, EmployeeDTO.class);

                empDTOList.add(empDTO);

           });

 

           empDTOList.forEach(p -> {

                System.out.println(p);

           });

 

     }

 

}

 

class EmployeeDo {

     @Override

     public String toString() {

           return "EmployeeDo [empId=" + empId + ", empName=" + empName + "]";

     }

 

     private int empId;

 

     /**

      * @return the empId

      */

     public int getEmpId() {

           return empId;

     }

 

     /**

      * @param empId the empId to set

      */

     public void setEmpId(int empId) {

           this.empId = empId;

     }

 

     /**

      * @return the empName

      */

     public String getEmpName() {

           return empName;

     }

 

     /**

      * @param empName the empName to set

      */

     public void setEmpName(String empName) {

           this.empName = empName;

     }

 

     private String empName;

 

}

 

class EmployeeDTO {

 

     private String empID;

 

     public String getEmpID() {

           return empID;

     }

 

     public void setEmpID(String empID) {

           this.empID = empID;

     }

 

     public String getEmpName() {

           return empName;

     }

 

     public void setEmpName(String empName) {

           this.empName = empName;

     }

 

     private String empName;

 

     @Override

     public String toString() {

           return "EmployeeDTO [empID=" + empID + ", empName=" + empName + "]";

     }

}

                                                  

 Output:

EmployeeDo [empId=22, empName=Prasad]

EmployeeDo [empId=32, empName=Bonam]

EmployeeDTO [empID=22, empName=Prasad]

EmployeeDTO [empID=32, empName=Bonam]

Search
Related Articles

Leave a Comment: