dozer是用来两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的set和get方法了。
下面我们来学习一下dozer的使用
1.添加依赖
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
2.创建配置类
package com.youyou.util.utils;
import org.dozer.DozerBeanMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
/**
* 各个模块的dozer配置文件
*/
@Configuration
public class DozerConfig {
@Bean(name = "org.dozer.Mapper")
public DozerBeanMapper dozer() {
//这里是配置文件的路径
List<String> mappingFiles = Arrays.asList("dozer/dozer-mapping.xml");
DozerBeanMapper dozerBean = new DozerBeanMapper();
dozerBean.setMappingFiles(mappingFiles);
return dozerBean;
}
}
3.创建两个POJO
创建DO
package com.youyou.address.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.youyou.base.BaseEntity;
import lombok.Data;
/**
* 数据库实体,联系人
*/
@Data
@TableName("TS_Contacter")
public class ContacterDO extends BaseEntity {
/**
* 姓名
*/
private String name;
/**
* 性别(0,女;1,男)
*/
private String sex;
/**
* 年龄
*/
private Integer age ;
/**
* 电话
*/
private String phone;
/**
* 地址
*/
private String location;
/**
* 删除(0,未删除;1,删除)
*/
private String dflag ;
}
DO的父类
package com.youyou.base;
import lombok.Data;
import java.util.Date;
/**
* 所有实体类的父类
*
* @author 刘朋
* <br/>date 2018-11-02
*/
@Data
public abstract class BaseEntity {
/**
* 主键
*/
private String id;
/**
* 创建时间
*/
private Date createTime;
}
创建VO
package com.youyou.address.vo;
import lombok.Data;
/**
* 联系人vo
*
* @author 刘朋
* <br/>date 2018-11-05
*/
@Data
public class ContacterVO {
/**
* 姓名
*/
private String name;
/**
* 性别(0,女;1,男)
*/
private String sex;
/**
* 年龄
*/
private Integer age ;
/**
* 电话
*/
private String phone;
/**
* 地址
*/
private String location;
}
4.创建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">
<!--如果两个类需要装换的属性名相同则不需要写这个mapping-->
<mapping>
<class-a>com.youyou.address.entity.ContacterDO</class-a>
<class-b>com.youyou.address.vo.ContacterVO</class-b>
</mapping>
</mappings>
5.使用
package com.youyou.address.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youyou.address.entity.ContacterDO;
import com.youyou.address.service.ContacterService;
import com.youyou.address.vo.ContacterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(description = "联系人接口")
@RestController
@RequestMapping(value = "/contacter")
public class ContacterController {
@Autowired
private ContacterService service;
@Autowired
private DozerBeanMapper dozerBeanMapper ;
@ApiOperation(value = "新增")
@GetMapping("/add")
public ContacterVO add(ContacterVO contacterVO) {
System.out.println("添加一个联系人");
//使用dozer进行转换
ContacterDO contacterDO = dozerBeanMapper.map(contacterVO, ContacterDO.class);
service.insertAutoKey(contacterDO);
return contacterVO;
}
}
搞定!
————————————————
版权声明:本文为CSDN博主「月月._.」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lp840312696/article/details/83748979