1. 程式人生 > 其它 >MapStruct文件(一)——簡介

MapStruct文件(一)——簡介

技術標籤:mapstructjava

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;
    }
}

結果

若是執行專案後沒有自動生成實現類,請手動執行mvn clean compile