Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Dozer in SpringBoot

Shulou Source: shulou.com Published: 2022-06-01 07:12:20 10月03日 Update

This article mainly shows you "how to use Dozer in SpringBoot", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use Dozer in SpringBoot" this article.

What is Dozer?

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. It is a powerful, general, flexible, reusable and configurable open source mapping framework.

To put it bluntly, dozer is a tool that can convert entities to entities. As long as the mapping relationship is established. It's like database and entity mapping for ORM.

Dozer is a tool for property conversion between two objects. With this tool, we don't have to write duplicate set and get methods when we transfer all property values of one object to another.

Let's learn about the use of dozer.

1. Add dependency net.sf.dozer dozer 5.5.1 2. Create a configuration class 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 file for each module * / @ Configurationpublic class DozerConfig {@ Bean (name = "org.dozer.Mapper") public DozerBeanMapper dozer () {/ / here is the path to the configuration file List mappingFiles = Arrays.asList ("dozer/dozer-mapping.xml"); DozerBeanMapper dozerBean = new DozerBeanMapper (); dozerBean.setMappingFiles (mappingFiles); return dozerBean;}} 3. Create two POJO

Create DO

Package com.youyou.address.entity; import com.baomidou.mybatisplus.annotation.TableName;import com.youyou.base.BaseEntity;import lombok.Data;/** * database entity, contact * / @ Data@TableName ("TS_Contacter") public class ContacterDO extends BaseEntity {/ * name * / private String name; * gender (0, female 1, male) private String sex; * age private Integer age; * phone private String phone; * address private String location; * deleted (0, not deleted; 1, deleted) private String dflag;}

Parent class of DO

Package com.youyou.base; import lombok.Data;import java.util.Date;/** * parent of all entity classes * * @ author Liu Peng * date 2018-11-02 * / @ Datapublic abstract class BaseEntity {/ * Primary key * / private String id; * creation time private Date createTime;}

Create VO

Package com.youyou.address.vo; import lombok.Data;/** * contact vo * * @ author Liu Peng * date 2018-11-05 * / @ Datapublic class ContacterVO {/ * * name * / private String name; * gender (0, female 1, male) private String sex; * age private Integer age; * telephone private String phone; * address private String location;} 4. Create the XML file com.youyou.address.entity.ContacterDO com.youyou.address.vo.ContacterVO 5. Use 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 = "contact interface") @ RestController@RequestMapping (value = "/ contacter") public class ContacterController {@ Autowired private ContacterService service; private DozerBeanMapper dozerBeanMapper @ ApiOperation (value = "add") @ GetMapping ("/ add") public ContacterVO add (ContacterVO contacterVO) {System.out.println ("add a contact"); / / use dozer to transform ContacterDO contacterDO = dozerBeanMapper.map (contacterVO, ContacterDO.class); service.insertAutoKey (contacterDO); return contacterVO }} above is all the content of the article "how to use Dozer in SpringBoot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Entity object contact contact configuration content tool data file article study between two address name attribute age gender database phone Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux OPPO Reno Shulou Information Huawei