I. Introduction to Dozer

Dozer is a Bean mapping tool that can recursively copy one object to another and is often used for conversion between VO, BO, and DO in a project.

Dozer provides a variety of mapping methods, such as implicit mapping, annotation mapping, XML mapping, and so on. In general, the default implicit mapping is sufficient, and annotation mapping or XML mapping can complicate Bean replication.

An implicit mapping is a mapping of the same attribute name between two beans, and the attribute’s access modifier does not affect the mapping of the Bean. If two beans have the same attribute name but different types, Dozer will type the beans according to the default conversion rules.

Second, integrate Dozer

Dozer in SpringBoot only needs to add the following dependencies to the pom.xml file:

<dependency>
    <groupId>com.github.dozermapper</groupId>
    <artifactId>dozer-core</artifactId>
    <version>6.5.0</version>
</dependency>
Copy the code

Implicit mapping

Implicit mapping simply calls the APIS provided by Dozer to complete the transformation of beans.

Dozer does not currently support mapping of collections; if a collection needs to be mapped, the objects in the collection need to be mapped one by one.

Here’s a simple encapsulation for Dozer:

/** * Dozer Mapper tool class */
public class LocalBeanUtil {
    private static final Mapper mapper = DozerBeanMapperBuilder.buildDefault();

    /** * Plain Bean copy *@param source
     * @param clazz
     * @param <T>
     * @param <D>
     * @return* /
    public static <T, D> D beanCopy(T source, Class<D> clazz) {
        if (source == null) {
            return null;
        }
        return mapper.map(source, clazz);
    }

    /** * collection object copy *@param source
     * @param clazz
     * @param <T>
     * @param <D>
     * @return* /
    public static <T, D> List<D> beanCopy(List<T> source, Class<D> clazz) {
        List<D> list = new ArrayList<D>();
        if(! CollectionUtils.isEmpty(source)) {for(T t : source) { D d = mapper.map(t, clazz); list.add(d); }}returnlist; }}Copy the code