kotlin之mapstruct集成

700 阅读1分钟

kotlin集成mapstruct

参考地址:
github.com/mapstruct/m…
github.com/mapstruct/m…

pom.xml配置

<?xml version="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
<modelVersion>4.0.0</modelVersion>  
<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.7.2</version>  
</parent>  
  
<groupId>com.go.demo</groupId>  
  
<artifactId>go-server</artifactId>  
<packaging>jar</packaging>  
  
<version>1.0-SNAPSHOT</version>  
  
  
<properties>  
    <jdk.version>1.8</jdk.version>  
    <kotlin.version>1.8.22</kotlin.version>  
    <mapstruct.version>1.5.5.Final</mapstruct.version>  
    <!--为了使您的构建更快,您可以通过定义kotlin.com.pier.incremental属性来启用Maven的增量编译-->  
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>  
    <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>  
</properties>  
  
<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-test</artifactId>  
        <scope>test</scope>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-validation</artifactId>  
    </dependency>  
    <!--kotlin--> 
    <dependency>  
        <groupId>org.jetbrains.kotlin</groupId>  
        <artifactId>kotlin-reflect</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.jetbrains.kotlin</groupId>  
        <artifactId>kotlin-stdlib</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.jetbrains.kotlinx</groupId>  
        <artifactId>kotlinx-serialization-json</artifactId>  
        <version>1.5.1</version>  
    </dependency>  
    <!--test-->  
    <dependency>  
        <groupId>org.jetbrains.kotlin</groupId>  
        <artifactId>kotlin-test-junit</artifactId>  
        <scope>test</scope>  
    </dependency>  

    <!--json工具类-->  
    <dependency>  
        <groupId>com.fasterxml.jackson.core</groupId>  
        <artifactId>jackson-databind</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>com.fasterxml.jackson.module</groupId>  
        <artifactId>jackson-module-jsonSchema</artifactId>  
        <version>2.13.3</version>  
    </dependency>  
    <!-- jackson 兼容 kotlin-->  
    <dependency>  
        <groupId>com.fasterxml.jackson.module</groupId>  
        <artifactId>jackson-module-kotlin</artifactId>  
        <version>2.13.3</version>  
    </dependency>  

    <!--mapstruct-->  
    <dependency>  
        <groupId>org.mapstruct</groupId>  
        <artifactId>mapstruct</artifactId>  
        <version>${mapstruct.version}</version>  
    </dependency>  

    <dependency>  
        <groupId>com.alibaba</groupId>  
        <artifactId>easyexcel</artifactId>  
        <version>3.3.2</version>  
        <exclusions>  
            <exclusion>  
                <artifactId>commons-io</artifactId>  
                <groupId>commons-io</groupId>  
            </exclusion>  
        </exclusions>  
    </dependency>  

    <dependency>  
        <groupId>org.projectlombok</groupId>  
        <artifactId>lombok</artifactId>  
        <scope>provided</scope>  
    </dependency>  
    <dependency>  
        <groupId>org.apache.commons</groupId>  
        <artifactId>commons-lang3</artifactId>  
        <version>3.12.0</version>  
    </dependency>  
    <dependency>  
        <groupId>commons-io</groupId>  
        <artifactId>commons-io</artifactId>  
        <version>2.12.0</version>  
    </dependency>  
    <dependency>  
        <groupId>commons-codec</groupId>  
        <artifactId>commons-codec</artifactId>  
        <version>1.15</version>  
    </dependency>  
    <dependency>  
        <groupId>com.alibaba.fastjson2</groupId>  
        <artifactId>fastjson2</artifactId>  
        <version>2.0.33</version>  
    </dependency>  
    <dependency>  
        <groupId>cn.hutool</groupId>  
        <artifactId>hutool-all</artifactId>  
        <version>5.8.20</version>  
    </dependency>  
</dependencies>  
  
<build>  
    <!--译源代码,指定源目录-->  
    <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>  
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
        </plugin>  
        <!--需要引用Kotlin Maven插件来编译源代码-->  
        <plugin>  
            <groupId>org.jetbrains.kotlin</groupId>  
            <artifactId>kotlin-maven-plugin</artifactId>  
            <version>${kotlin.version}</version>  
            <configuration>  
                <args>  
                    <arg>-Xjsr305=strict</arg>  
                </args>  
                <compilerPlugins>  
                    <plugin>lombok</plugin>  
                    <plugin>spring</plugin>  
                    <plugin>all-open</plugin>  
                </compilerPlugins>  
                <pluginOptions>  
                    <option>all-open:annotation=javax.persistence.Entity</option>  
                    <option>all-open:annotation=javax.persistence.Embeddable</option>  
                    <option>all-open:annotation=javax.persistence.MappedSuperclass</option>  
                </pluginOptions>  
                <jvmTarget>${jdk.version}</jvmTarget>  
            </configuration>  
            <dependencies>  
                <dependency>  
                    <groupId>org.jetbrains.kotlin</groupId>  
                    <artifactId>kotlin-maven-lombok</artifactId>  
                    <version>${kotlin.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.projectlombok</groupId>  
                    <artifactId>lombok</artifactId>  
                    <version>${lombok.version}</version>  
                    <scope>compile</scope>  
                </dependency>  
                <dependency>  
                    <groupId>org.jetbrains.kotlin</groupId>  
                    <artifactId>kotlin-maven-allopen</artifactId>  
                    <version>${kotlin.version}</version>  
                </dependency>  
            </dependencies>  
            <executions>  
                <execution>  
                    <id>kapt</id>  
                    <goals>  
                        <goal>kapt</goal> <!-- You can skip the <goals> element if you enable extensions for the plugin -->  
                    </goals>  
                    <configuration>  
                        <sourceDirs>  
                            <sourceDir>src/main/kotlin</sourceDir>  
                            <sourceDir>src/main/java</sourceDir>  
                        </sourceDirs>  
                        <annotationProcessorPaths>  
                            <annotationProcessorPath>  
                                <groupId>org.mapstruct</groupId>  
                                <artifactId>mapstruct-processor</artifactId>  
                                <version>${mapstruct.version}</version>  
                            </annotationProcessorPath>  
                        </annotationProcessorPaths>  
                        <annotationProcessorArgs>  
                            <arg>mapstruct.defaultComponentModel=spring</arg>  
                            <arg>mapstruct.unmappedTargetPolicy=ERROR</arg>  
                        </annotationProcessorArgs>  
                    </configuration>  
                </execution>  
                <execution>  
                    <id>compile</id>  
                    <phase>compile</phase>  
                    <goals>  
                        <goal>compile</goal>  
                    </goals>  
                </execution>  
                <execution>  
                    <id>test-compile</id>  
                        <phase>test-compile</phase>  
                    <goals>  
                        <goal>test-compile</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>  
        <!--maven编译插件-->  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>3.8.1</version>  
            <configuration>  
                <proc>none</proc>  
                <source>${kotlin.compiler.jvmTarget}</source>  
                <target>${kotlin.compiler.jvmTarget}</target>  
            </configuration>  
            <executions>  
                <!-- 替换默认编译,因为它是由 Maven 特别处理的 -->  
                <execution>  
                    <id>default-compile</id>  
                    <phase>none</phase>  
                </execution>  
                <!-- 替换 default-testCompile,因为它被 Maven 特别处理 -->  
                <execution>  
                    <id>default-testCompile</id>  
                    <phase>none</phase>  
                </execution>  
                <execution>  
                    <id>java-compile</id>  
                        <phase>compile</phase>  
                    <goals>  
                        <goal>compile</goal>  
                    </goals>  
                </execution>  
                <execution>  
                    <id>java-test-compile</id>  
                        <phase>test-compile</phase>  
                    <goals>  
                        <goal>testCompile</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  
</project>

StudentMapper

package com.gjw.go.mapstruct  
  
import com.gjw.go.domain.entity.StudentPo  
import com.gjw.go.excel.listener.student.StudentImpDto  
import org.mapstruct.Mapper  
import org.mapstruct.Mapping  
import org.mapstruct.factory.Mappers  
  
/**  
* 参考地址:  
* https://github.com/mapstruct/mapstruct/issues/1672  
* https://github.com/mapstruct/mapstruct-examples  
*/
@Mapper  
interface StudentMapper {  
    companion object {  
        var INSTANCE = Mappers.getMapper(StudentMapper::class.java)  
    }  
    
    //@Mappings无法使用
    @Mapping(source = "schoolName", target = "school")  
    @Mapping(source = "teacherName", target = "teacher")  
    fun studentImpDtoToStudentPo(studentImpDto: StudentImpDto): StudentPo  
}

MapstructTest

package com.test.service  
  
import com.alibaba.fastjson2.JSON  
import com.gjw.go.excel.listener.student.StudentImpDto  
import com.gjw.go.mapstruct.StudentMapper  
import java.time.LocalDateTime  
import kotlin.test.Test  
  
class MapstructTest {  
    @Test  
    fun mainssss() {  
        var studentImpDto = StudentImpDto().apply {  
            name = "小明"  
            age = 18  
            schoolName = "中山大学"  
            clzz = "初一三班"  
            teacherName = "刘能"  
            createTime = LocalDateTime.now()  
        }  

        println("before studentImpDto = " + JSON.toJSONString(studentImpDto))  
        var studentPo = StudentMapper.INSTANCE.studentImpDtoToStudentPo(studentImpDto)  
        println("after studentPo = " + JSON.toJSONString(studentPo))  
    }  
}