SpringBoot 整合mapstruct

墨尘 94 0

引入依赖

<?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 https://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.6.0</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.ams</groupId>

    <artifactId>springboot-mapstruct</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>springboot-mapstruct</name>

    <description>Demo project for Spring Boot</description>

    <properties>

        <java.version>1.8</java.version>

        <org.mapstruct>1.4.1.Final</org.mapstruct>

    </properties>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>


        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>


  <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok-mapstruct-binding</artifactId>

            <version>0.2.0</version>

        </dependency>

————————————————

版权声明:本文为CSDN博主「AI码师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_34311210/article/details/121577055



        <!-- mapStruct 对象转换 -->

        <dependency>

            <groupId>org.mapstruct</groupId>

            <artifactId>mapstruct</artifactId>

            <version>${org.mapstruct}</version>

        </dependency>

        <dependency>

            <groupId>org.mapstruct</groupId>

            <artifactId>mapstruct-processor</artifactId>

            <version>${org.mapstruct}</version>

        </dependency>

<!--        不是必备 只是为了懒,不用写get set方法-->

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

            <optional>true</optional>

        </dependency>


        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok-mapstruct-binding</artifactId>

            <version>0.2.0</version>

        </dependency>

    </dependencies>


    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <excludes>

                        <exclude>

                            <groupId>org.projectlombok</groupId>

                            <artifactId>lombok</artifactId>

                        </exclude>

                    </excludes>

                </configuration>

            </plugin>

        </plugins>

    </build>


</project>


创建 DTO、VO

StudentDto

package com.ams.springbootmapstruct.dto;


import lombok.Data;


/**

 * Created with IntelliJ IDEA.

 *

 * @author: AI码师

 * @date: 2021/11/27

 * @description:

 * @modifiedBy:

 * @version: 1.0

 */

@Data

public class StudentDto {

    private String userName;

    private String userId;

    private String address;

    private String school;

    private int age;

    private String email;


}

StudenVo

package com.ams.springbootmapstruct.vo;


import lombok.Builder;

import lombok.Data;


/**

 * Created with IntelliJ IDEA.

 *

 * @author: AI码师

 * @date: 2021/11/27

 * @description:

 * @modifiedBy:

 * @version: 1.0

 */

@Data

@Builder

public class StudentVo {

    private String userName;

    private String userId;

    private String address;

    private String school;

    private int age;

    private String emailAddress;

}


创建mapstruct转换器

package com.ams.springbootmapstruct.mapper;


import com.ams.springbootmapstruct.dto.StudentDto;

import com.ams.springbootmapstruct.vo.StudentVo;

import org.mapstruct.Mapper;


@Mapper(componentModel = "spring")

public interface MainMapper {

    StudentDto studentVo2Dto(StudentVo vo);

}

编写测试用例

package com.ams.springbootmapstruct;


import com.ams.springbootmapstruct.dto.StudentDto;

import com.ams.springbootmapstruct.mapper.MainMapper;

import com.ams.springbootmapstruct.vo.StudentVo;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest

class SpringbootMapstructApplicationTests {

    @Autowired

    private MainMapper mainMapper;


    @Test

    void testSimpleMap() {

        StudentVo studentVo = StudentVo.builder()

                .school("清华大学")

                .userId("ams")

                .userName("AI码师")

                .age(27)

                .address("合肥")

                .build();

        StudentDto studentDto = mainMapper.studentVo2Dto(studentVo);

        System.out.println(studentDto);

    }

}


mapstruct常规操作

不同字段映射

如果两个实体中 有几个字段命名不一致,可以使用@Mapping 解决
现在studenVo和studenDto 有email 和emailAddress 字段不一致,可以使用如下方式解决

package com.ams.springbootmapstruct.mapper;


import com.ams.springbootmapstruct.dto.StudentDto;

import com.ams.springbootmapstruct.vo.StudentVo;

import org.mapstruct.Mapper;

import org.mapstruct.Mapping;


@Mapper(componentModel = "spring")

public interface MainMapper {

    @Mapping(source = "emailAddress", target = "email")

    StudentDto studentVo2Dto(StudentVo vo);

}


LIST转换

package com.ams.springbootmapstruct.mapper;


import com.ams.springbootmapstruct.dto.StudentDto;

import com.ams.springbootmapstruct.vo.StudentVo;

import org.mapstruct.Mapper;

import org.mapstruct.Mapping;


import java.util.List;


@Mapper(componentModel = "spring")

public interface MainMapper {

    @Mapping(source = "emailAddress", target = "email")

    StudentDto studentVo2Dto(StudentVo vo);

    List<StudentDto> studentListVo2Dto(List<StudentVo> vo);

}





————————————————

版权声明:本文为CSDN博主「AI码师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_34311210/article/details/121577055


标签: #java #mapstruct

上一篇Echarts入门

下一篇Java学习路线

  • 评论列表

留言评论