Dozer的使用(整合springboot)

dozer是实体类与实体类之间转换的工具,下面是简单操作配置

引入依赖:

<!--dozer 实体类转换工具-->
        <dependency>
            <groupId>net.sf.dozer</groupId>
            <artifactId>dozer-spring</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.dozer</groupId>
            <artifactId>dozer</artifactId>
            <version>5.5.1</version>
        </dependency>

首先是dozer的配置,写配置类

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author zxy
 * @Classname DozerMapperConfig
 * @Date 2022/3/29 16:10
 */
@Configuration
public class DozerMapperConfig {

    @Bean(name = "org.dozer.Mapper")
    public Mapper mapper(){
        DozerBeanMapper mapper = new DozerBeanMapper();
        //如果要读取多个或者一个xml文件,则将写进下面地址,如果不需要xml配置则不用。
        // mapper.setMappingFiles(Arrays.asList("dozer/dozer-mapping.xml","dozer/xxx.xml"));
        return mapper;
    }
}

接下来封装一下转换工具


import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.dozer.Mapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
 * @author zxy
 * @Classname GeneralConvertor
 * @Date 2022/3/29 15:21
 */
@Component
public class GeneralConvertor {

   @Resource
   Mapper mapper;

    /**
     * List  实体类 转换器
     * @return
     */
    public <T, S> List<T> convertor(List<S> source, Class<T> clz) {
        if (source == null) return null;
        List<T> map = new ArrayList<>();
        for (S s : source) {
            map.add(mapper.map(s, clz));
        }
        return map;
    }

    /**
     * Set 实体类 深度转换器
     * @return
     */
    public <T, S> Set<T> convertor(Set<S> source, Class<T> clz) {
        if (source == null) return null;
        Set<T> set = new TreeSet<>();
        for (S s : source) {
            set.add(mapper.map(s, clz));
        }
        return set;
    }

    /**
     * 实体类 深度转换器
     * @return
     */
    public <T, S> T convertor(S source, Class<T> clz) {
        if (source == null) return null;
        return mapper.map(source, clz);
    }

    public void convertor(Object source, Object object) {
        mapper.map(source, object);
    }

    public <T> void copyConvertor(T source, Object object) {
        mapper.map(source, object);
    }

}


 接下来是dozer的xml配置,但是我没有使用,

<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dozer.sourceforge.net"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping>
        <!--对应的实体和要转换成的VO或者使用注解@Mapping,使用注解就不用配置类了-->
        <!--@Mapping 用在字段名不一样的地方,下面的field同理-->
        <class-a>com.jntech.model.sqlserver2.Employee</class-a>
        <class-b>com.jntech.vo.EmployeeVO</class-b>
        <!--对应要转换的属性名.<a>和<class-a>对应,其它同理-->
        <field>
            <a>employeeid</a>
            <b>id</b>
        </field>
        <field>
            <a>emaddress</a>
            <b>address</b>
        </field>

    </mapping>
</mappings>

然后创建两个实体类测试

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>
 * 
 * </p>
 *
 * @author zxy
 * @since 2022-03-23
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="User对象", description="")
@TableName("user")
public class UserDO implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "UUID用户id")
    @TableId(type = IdType.ASSIGN_UUID)
    private String userId;

    @ApiModelProperty(value = "登录用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "用户信息id")
    private String userInfoId;

    @ApiModelProperty(value = "用户权限")
    private String role;

    @ApiModelProperty(value = "创建时间")
    private Long ctime;

    @ApiModelProperty(value = "修改时间")
    private Long mtime;


}
import lombok.Data;

/**
 * @author zxy
 * @Classname UserVO
 * @Date 2022/3/29 15:18
 */
@Data
public class UserVO {

    private String userId;

    private String username;

    private String userInfoId;

    private String Role;
}

最后进行单元测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值