在春季启动中,有很多不同的方法可以将DTO映射到实体,反之亦然,我们可以使用Modelmapper,Mapstruct,Dozermapper。
按照完整的文章了解如何使用模型映射器将 DTO 映射到实体对象。
创建 Spring 引导项目
有许多不同的方法可以创建一个 Spring 启动应用程序,您可以按照以下文章创建一个 –
>> 使用 Spring 初始值设定项
创建 Spring 启动应用程序>>在 Spring 工具套件中创建 Spring 启动应用程序 [STS]
>>在 IntelliJ IDEA 中创建 Spring 启动应用程序
添加 maven 依赖项
打开pom.xml并添加以下依赖项 –
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.5.5version>
- <relativePath />
- parent>
- <groupId>in.bushansirgurgroupId>
- <artifactId>springbootappartifactId>
- <version>1.0.0version>
- <name>springbootappname>
- <description>Spring boot appdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.modelmappergroupId>
- <artifactId>modelmapperartifactId>
- <version>0.7.4version>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
spring-boot-starter-web依赖项,用于使用 Spring MVC 构建 Web 应用程序。它使用 tomcat 作为默认的嵌入式容器。org.modelmapper依赖项用于使用模型映射器将 DTO 映射到实体以及将实体映射到 DTO。
创建 DTO 类
在in.bushansirgur.dto包中创建EmployeeDTO.java并添加以下内容
- package in.bushansirgur.springbootapp.entity;
-
- public class EmployeeDTO {
-
- private String name;
-
- private int age;
-
- private String location;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getLocation() {
- return location;
- }
-
- public void setLocation(String location) {
- this.location = location;
- }
-
- }
创建实体类
在in.bushansirgur.entity包中创建员工.java并添加以下内容
- package in.bushansirgur.springbootapp.entity;
-
- public class Employee {
-
- private String name;
-
- private int age;
-
- private String location;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getLocation() {
- return location;
- }
-
- public void setLocation(String location) {
- this.location = location;
- }
-
- @Override
- public String toString() {
- return "Employee [name=" + name + ", age=" + age + ", location=" + location + "]";
- }
-
- }
创建控制器
创建HomeController.java在in.bushansirgur.Controller包中添加以下内容
- package in.bushansirgur.springbootapp.controller;
-
- import org.modelmapper.ModelMapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RestController;
-
- import in.bushansirgur.springbootapp.entity.Employee;
- import in.bushansirgur.springbootapp.model.EmployeeDTO;
-
- @RestController
- public class HomeController {
-
- @Autowired
- private ModelMapper mapper;
-
- @PostMapping("/save")
- public Employee save(@RequestBody EmployeeDTO employeeDto) {
- Employee employee = mapper.map(employeeDto, Employee.class);
- System.out.println("Printing employee after mapping:"+employee);
- return employee;
- }
- }
使用以下 maven 命令运行应用程序 –
mvn spring-boot:run
打开浏览器并输入以下网址 –

http://localhost:8080/save 这篇文章就是这样