mapstruct是一个java的bena对象转换映射工具。只需定义mapper接口,会在编译时动态的生成进行set/get操作的class实现类文件,在运行时直接调用。速度快,其他性能指标也很好。
需要配置的mavne设置
...
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.1.Final</version>
</dependency>
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 使用lombok注意冲突,maven-compiler-plugin要3.6以上,lombok使用1.16.16版本以上,需要配置lombok的path -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
<!-- 此配置的意思 详见第10章 -->
<path>
<groupId>com.haru.acfun</groupId>
<artifactId>spi</artifactId>
<version>1.0-SNAPSHOT</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
...
定义之后会使用到的类
@Data
@ToString
public class TestPO {
private Long id;
private String name;
private BigDecimal price;
private Date creteTime;
}
@Data
@ToString
public class TestTwoPO {
private Long id;
private String title;
private Float totalPrice;
}
@Data
@ToString
public class TestBO {
private Long id;
private String name;
private BigDecimal price;
private Date creteTime;
}
@Data
@ToString
public class TestMixBO {
private Long id;
private String name;
private BigDecimal price;
private Date creteTime;
private String title;
}
@Data
@ToString
public class TestThreePO {
private TestPO test;
}
@Data
@ToString
public class TestThreePO {
private TestPO test;
private BigDecimal totalPrice;
}
@Data
public class TestFourBO {
private TestFiveBO test;
private String totalPrice;
}
@Data
@ToString
public class TestFiveBO {
private Long idFive;
private String name;
private String price;
private String title;
private Date creteTime;
}
@Data
@ToString
public class BaseBO {
private Long id;
private String name;
}
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class TestSixBO extends BaseBO {
}
@Data
@ToString
public class TestSevenBO {
private TestSixBO test;
}
@Data
@ToString
public class BasePO {
private Long id;
}
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
public class TestFourPO extends BasePO {
}
@Data
public class TestFivePO {
private TestFourPO test;
}
简单使用
@Mapper // 添加注解
public interface TestMapper {
TestBO testToBO(TestPO testPO);
TestPO testToP0(TestBO testBO);
List<TestBO> testToBOS(List<TestPO> testPOS);
List<TestPO> testToP0S(List<TestBO> testBOS);
}
@Test
public void baseTest() {
List<TestPO> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
TestPO testPO = new TestPO();
testPO.setId(1L);
testPO.setName("haru");
testPO.setPrice(new BigDecimal("12.02"));
testPO.setCreteTime(new Date(System.currentTimeMillis()));
list.add(testPO);
}
TestMapper mapper = Mappers.getMapper(TestMapper.class); // 获取mapper
List<TestBO> testBOS = mapper.testToBOS(list); // 转换
System.out.println(testBOS);
}
编译后自动生成的实现类
import com.ljc.dozer.bo.TestBO;
import com.ljc.dozer.po.TestPO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-10-28T14:06:53+0800",
comments = "version: 1.4.1.Final, compiler: javac, environment: Java 1.8.0_251 (Oracle Corporation)"
)
public class TestMapperImpl implements TestMapper {
@Override
public TestBO testToBO(TestPO testPO) {
if ( testPO == null ) {
return null;
}
TestBO testBO = new TestBO();
testBO.setId( testPO.getId() );
testBO.setName( testPO.getName() );
testBO.setPrice( testPO.getPrice() );
testBO.setCreteTime( testPO.getCreteTime() );
return testBO;
}
@Override
public TestPO testToP0(TestBO testBO) {
if ( testBO == null ) {
return null;
}
TestPO testPO = new TestPO();
testPO.setId( testBO.getId() );
testPO.setName( testBO.getName() );
testPO.setPrice( testBO.getPrice() );
testPO.setCreteTime( testBO.getCreteTime() );
return testPO;
}
@Override
public List<TestBO> testToBOS(List<TestPO> testPOS) {
if ( testPOS == null ) {
return null;
}
List<TestBO> list = new ArrayList<TestBO>( testPOS.size() );
for ( TestPO testPO : testPOS ) {
list.add( testToBO( testPO ) );
}
return list;
}
@Override
public List<TestPO> testToP0S(List<TestBO> testBOS) {
if ( testBOS == null ) {
return null;
}
List<TestPO> list = new ArrayList<TestPO>( testBOS.size() );
for ( TestBO testBO : testBOS ) {
list.add( testToP0( testBO ) );
}
return list;
}
}
结果
@Mapper:若映射的对象字段名相同,只需在要映射的接口上添加此注解就可以了;@Mapping用于指定映射的字段,可以设置映射的字段名等,后续会说明。
若是运行项目后没有自动生成实现类,请手动执行mvn clean compile。