Many times in programming language we are copying attributes of one class to other class.
Most of the time we are doing same thing more than one places so its repeating code. Dozer has provided API to do same ting in very easy way.
Step 1: Create Project
Create Spring Project.Please refer below link to create project
https://sagarthakare1.wordpress.com/2015/04/23/spring-hello-world-example-with-maven/?preview=true&preview_id=38&preview_nonce=1e8a5eead7
Step 2:Create Employee class
File:Employee.java
package com.javatpoint;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
public class Employee {
private int id;
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Step:4 Create Student class
File:Student.java
package com.javatpoint;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 4:Create DozerMapping file
File:DozerMapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>MM/dd/yyyy HH:mm</date-format>
<wildcard>true</wildcard>
</configuration>
<mapping>
<class-a>com.javatpoint.Student</class-a>
<class-b>com.javatpoint.Employee</class-b>
<field>
<a>name</a>
<b>firstName</b>
</field>
</mapping>
</mappings>
Step 5: Create Bean
We are creating bean with spring so we will add this in applicationContext.xml file.
File:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.javatpoint" />
<bean id="mapper" class="org.dozer.DozerBeanMapper">
<property name="mappingFiles">
<list>
<value>DozerMapping.xml</value>
</list>
</property>
</bean>
</beans>
Step 6 :Create runner class
Create class to run this code
I have create insertTest class
File:InsertTest.java
package com.javatpoint.impl;
import java.util.Arrays;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javatpoint.Employee;
import com.javatpoint.Student;
public class InsertTest {
public static void main(String[] args) {
Student s= new Student();
s.setName("Alexander");
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
Mapper mapper=(DozerBeanMapper)context.getBean("mapper");
//DozerBeanMapper mapper = new DozerBeanMapper(Arrays.asList(new String[]{"DozerMapping.xml"}));
Employee employee=mapper.map(s, Employee.class);
System.out.println("Name Mapped by dozer is:"+employee.getFirstName());
}
}
Step 7 :Output
You will get below output after running code
Name Mapped by dozer is:Alexander
Most of the time we are doing same thing more than one places so its repeating code. Dozer has provided API to do same ting in very easy way.
Step 1: Create Project
Create Spring Project.Please refer below link to create project
https://sagarthakare1.wordpress.com/2015/04/23/spring-hello-world-example-with-maven/?preview=true&preview_id=38&preview_nonce=1e8a5eead7
Step 2:Create Employee class
File:Employee.java
package com.javatpoint;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
public class Employee {
private int id;
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Step:4 Create Student class
File:Student.java
package com.javatpoint;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 4:Create DozerMapping file
File:DozerMapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>MM/dd/yyyy HH:mm</date-format>
<wildcard>true</wildcard>
</configuration>
<mapping>
<class-a>com.javatpoint.Student</class-a>
<class-b>com.javatpoint.Employee</class-b>
<field>
<a>name</a>
<b>firstName</b>
</field>
</mapping>
</mappings>
Step 5: Create Bean
We are creating bean with spring so we will add this in applicationContext.xml file.
File:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.javatpoint" />
<bean id="mapper" class="org.dozer.DozerBeanMapper">
<property name="mappingFiles">
<list>
<value>DozerMapping.xml</value>
</list>
</property>
</bean>
</beans>
Step 6 :Create runner class
Create class to run this code
I have create insertTest class
File:InsertTest.java
package com.javatpoint.impl;
import java.util.Arrays;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javatpoint.Employee;
import com.javatpoint.Student;
public class InsertTest {
public static void main(String[] args) {
Student s= new Student();
s.setName("Alexander");
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
Mapper mapper=(DozerBeanMapper)context.getBean("mapper");
//DozerBeanMapper mapper = new DozerBeanMapper(Arrays.asList(new String[]{"DozerMapping.xml"}));
Employee employee=mapper.map(s, Employee.class);
System.out.println("Name Mapped by dozer is:"+employee.getFirstName());
}
}
Step 7 :Output
You will get below output after running code
Name Mapped by dozer is:Alexander
No comments:
Post a Comment