- MapStruct 教程
- MapStruct - 首页
- MapStruct - 概述
- MapStruct - 环境搭建
- 映射
- MapStruct - 基本映射
- MapStruct - 自定义映射
- MapStruct - 多映射
- MapStruct - 嵌套Bean映射
- MapStruct - 直接字段映射
- MapStruct - 构造器
- 数据类型转换
- MapStruct - 隐式类型转换
- MapStruct - 使用 numberFormat
- MapStruct - 使用 dateFormat
- MapStruct - 使用表达式
- MapStruct - 使用常量
- MapStruct - 使用 defaultValue
- MapStruct - 使用 defaultExpression
- 集合映射
- MapStruct - 列表映射
- MapStruct - Map映射
- 其他
- MapStruct - 流映射
- MapStruct - 枚举映射
- MapStruct - 抛出异常
- MapStruct - 自定义映射器
- MapStruct 有用资源
- MapStruct - 快速指南
- MapStruct - 有用资源
- MapStruct - 讨论
MapStruct - 隐式类型转换
MapStruct 在大多数情况下会自动处理类型转换。例如,int 到 Long 或 String 的转换。转换也处理空值。以下是一些重要的自动转换。
基本类型和对应的包装类之间。
基本类型和字符串之间。
枚举类型和字符串之间。
BigInt、BigDecimal 和字符串之间。
Calendar/Date 和 XMLGregorianCalendar 之间。
XMLGregorianCalendar 和字符串之间。
Joda 日期类型和字符串之间。
示例
在 Eclipse 中打开项目映射,如使用构造器的映射章节中更新的那样。
使用以下代码更新 StudentEntity.java:
StudentEntity.java
package com.tutorialspoint.entity;
public class StudentEntity {
private String id;
private String name;
private String classVal;
private SubjectEntity subject;
public String section;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassVal() {
return classVal;
}
public void setClassVal(String classVal) {
this.classVal = classVal;
}
public SubjectEntity getSubject() {
return subject;
}
public void setSubject(SubjectEntity subject) {
this.subject = subject;
}
}
Student.java 代码不变,如下:
Student.java
package com.tutorialspoint.model;
public class Student {
private final String name;
private final int id;
protected Student(Student.Builder builder) {
this.name = builder.name;
this.id = builder.id;
}
public static Student.Builder builder() {
return new Student.Builder();
}
public static class Builder {
private String name;
private int id;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder id(int id) {
this.id = id;
return this;
}
public Student create() {
return new Student( this );
}
}
public String getName() {
return name;
}
public int getId() {
ret+urn id;
}
}
使用以下代码更新 DeliveryAddressMapperTest.java:
DeliveryAddressMapperTest.java
package com.tutorialspoint.mapping;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.AddressEntity;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.mapper.DeliveryAddressMapper;
import com.tutorialspoint.model.DeliveryAddress;
public class DeliveryAddressMapperTest {
private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class);
@Test
public void testEntityToModel() {
StudentEntity student = new StudentEntity();
student.setClassVal("X");
student.setName("John");
student.setId("1");
AddressEntity address = new AddressEntity();
address.setCity("Y");
address.setState("Z");
address.setHouseNo(1);
DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddress(student, address);
assertEquals(deliveryAddress.getName(), student.getName());
assertEquals(deliveryAddress.getCity(), address.getCity());
assertEquals(deliveryAddress.getState(), address.getState());
assertEquals(deliveryAddress.getHouseNumber(), address.getHouseNo());
}
}
使用以下代码更新 StudentMapperTest.java:
StudentMapperTest.java
package com.tutorialspoint.mapping;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.entity.SubjectEntity;
import com.tutorialspoint.mapper.StudentMapper;
import com.tutorialspoint.model.Student;
public class StudentMapperTest {
private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
@Test
public void testEntityToModel() {
StudentEntity entity = new StudentEntity();
entity.setName("John");
entity.setId("1");
Student model = studentMapper.getModelFromEntity(entity);
assertEquals(entity.getName(), model.getName());
assertEquals(Integer.parseInt(entity.getId()), model.getId());
}
@Test
public void testModelToEntity() {
Student.Builder builder = Student.builder().id(1).name("John");
Student model = builder.create();
StudentEntity entity = studentMapper.getEntityFromModel(model);
assertEquals(entity.getName(), model.getName());
assertEquals(Integer.parseInt(entity.getId()), model.getId());
}
}
运行以下命令测试映射。
mvn clean test
输出
命令成功后。验证输出。
mvn clean test [INFO] Scanning for projects... ... [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping --- [INFO] Surefire report directory: \mvn\mapping\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.tutorialspoint.mapping.DeliveryAddressMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec Running com.tutorialspoint.mapping.StudentMapperTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 ...
广告