One of Java's mapping frameworks. Dozer is a mapping framework that uses recursion to copy data from one object to another. The framework can not only copy properties between beans, but also automatically convert between different types.
Describe the following in the "build.gradle" file
Description location: dependencies Description code:
implementation 'net.sf.dozer:dozer:5.5.1'
Add the following to pom.xml
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
You can read the mapping xml to be written by adding the following to application.context.
<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles"
value="classpath*:/META-INF/dozer/**/*-mapping.xml" /><!-- (1) -->
</bean>
First, let's compare the code description with and without Dozer.mapper.
Duzer.No mapper
input.setId(context.getId());
input.setName(context.getName());
input.setTitle(context.getTitle());
input.setSubtitlle(context.getSubTitle());
input.setBusinessDate(context.getBusinessDate());
Dozer.with mapper
Mapper mapper = new DozerBeanMapper();
mapper.map(context, input);
As you can see at a glance, the description has been largely omitted. This time there is no hierarchical structure of been, but even so, you do not have to write getters and setters many times to prevent omission of value copying.
I will explain in detail when I see the outline.
Been used this time Map this to the been required to call Service.
@Data
public class ControllerContext {
private int id;
private String name;
private String title;
private String subTitle;
private Date businessDate;
}
@Data
public class ServiceContext {
private int id;
private String name;
private String title;
private String subtitlle;
private Date businessDate;
}
Set the value to become of the controller as follows. Then map with Dozer.mapper.
ServiceContext input = new ServiceContext();
context.setId(2);
context.setName("Good");
context.setTitle("Title 2");
context.setSubTitle("Subtitle 2");
context.setBusinessDate(DateUtils.addDays(new Date(), 1));
Mapper mapper = new DozerBeanMapper();
mapper.map(context, input);
System.out.println(input);
Output result
ServiceContext(id=2, name=Good, title=Title 2, subtitlle=subtitle, businessDate=Mon Oct 19 00:17:38 JST 2020)
Method | Description |
---|---|
mapper.map() | It can be used by describing the converted source name in the first argument and the converted been name in the second argument. |
You can see that it is mapped normally. In this way, the description of been mapping can be omitted and the program can be written concisely.
Dozer's default settings automatically copy properties that have the same name as the source and destination. Custom mapping is required to copy properties with different names.
Dozer settings can be defined in either XML or Java Config. It is OK if you use the one you want to use, but this time I will describe the description in the case of XML.
If you have done the setup for the first Maven project, the `-mapping.xml``` format file under
`resouce / META-INF / dozer``` will be read at the time of mapping. It has become.
The mapping XML is described as follows.
<?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">
<!-- omitted -->
<mapping>
<class-a>com.example.mavenDemo.controller.ControllerForm</class-a>
<class-b>com.example.mavenDemo.service.ServiceContext</class-b>
<field>
<a>controllerTitle</a>
<b>title</b><!-- (1) -->
</field>
</mapping>
<!-- omitted -->
</mappings>
tag | Description |
---|---|
class-a | |
class-b | |
a | <field> In the tag<a> Specify the field name for mapping of the copy source bean in the tag. |
b | <field> In the tag<b> Class in the tag-Specify the field name for mapping of the copy destination bean corresponding to a. |
The above custom mapping settings alleviate the difference in field names.
Even if the data type is different, if it is an existing pattern, it will be saved automatically. See below for conversion patterns. Basic Property Mapping
Let's take a mapping example in which id
of the mapping given earlier is changed to int
.
@Data
public class ControllerForm {
private String id;
private String name;
private String title;
private String subTitle;
private Date businessDate;
}
@Data
public class ServiceContext {
private int id;
private String name;
private String title;
private String subTitle;
private Date businessDate;
}
Mapper mapper = new DozerBeanMapper();
mapper.map(context, input);
Execution result
ServiceContext(id=2, name=Good, title=Title 2, subTitle=サブTitle 2, businessDate=Mon Oct 19 17:12:25 JST 2020)
It can be seen that mapping is performed normally even when the data types are different.
In this article, I could only introduce the touch part of Dozer mapping. See the reference article for more details.
Java Mapping Framework Performance --Codeflow Spring Boot Camp: Spring Boot + Dozer Edition How to use Dozer
Recommended Posts