在Spring Boot项目中整合MapStruct,可以方便地实现对象之间的数据类型转换。MapStruct是一个代码生成器,它通过注解的方式自动生成映射代码,从而避免了手动编写重复的转换逻辑。以下是详细的步骤和代码示例:
1. 添加依赖
首先,在你的pom.xml
文件中添加MapStruct相关的依赖:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MapStruct Dependencies -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
2. 创建源类和目标类
定义两个简单的Java类,一个作为源类,另一个作为目标类:
// UserDTO.java (Source Class)
public class UserDTO {
private String name;
private int age;
// Getters and Setters
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;
}
}
// UserEntity.java (Target Class)
public class UserEntity {
private String fullName;
private int age;
// Getters and Setters
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. 创建Mapper接口
创建一个Mapper接口,并使用MapStruct的注解来定义映射规则:
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Mapping(source = "name", target = "fullName")
UserEntity userDtoToUserEntity(UserDTO userDTO);
}
在这个例子中,我们使用了@Mapping
注解来指定字段的映射关系。例如,将UserDTO
中的name
字段映射到UserEntity
中的fullName
字段。
4. 使用Mapper进行转换
在你的服务或控制器中使用Mapper来进行对象转换:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/convert")
public UserEntity convertUser() {
UserDTO userDTO = new UserDTO();
userDTO.setName("John Doe");
userDTO.setAge(30);
UserEntity userEntity = UserMapper.INSTANCE.userDtoToUserEntity(userDTO);
return userEntity;
}
}
5. 启动应用并测试转换功能
启动你的Spring Boot应用程序,然后在浏览器中访问以下URL:
http://localhost:8080/api/convert
你应该能够看到返回的JSON对象,其中包含转换后的数据:
{
"fullName": "John Doe",
"age": 30
}
6. 自定义映射规则(可选)
你可以进一步自定义映射规则,例如处理嵌套对象、集合等。以下是一个更复杂的示例:
// AddressDTO.java (Nested Source Class)
public class AddressDTO {
private String street;
private String city;
// Getters and Setters
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
// AddressEntity.java (Nested Target Class)
public class AddressEntity {
private String addressLine;
private String location;
// Getters and Setters
public String getAddressLine() {
return addressLine;
}
public void setAddressLine(String addressLine) {
this.addressLine = addressLine;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
更新Mapper接口以支持嵌套对象的映射:
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Mapping(source = "name", target = "fullName")
@Mapping(source = "address", target = "addressEntity") // Nested mapping example
UserEntity userDtoToUserEntity(UserDTO userDTO);
}
通过以上步骤,你已经成功地在Spring Boot项目中整合了MapStruct,并实现了对象之间的数据类型转换。这样,你可以更方便地进行对象之间的转换操作。