当前位置: 首页 > 知识库问答 >
问题:

我的mapperImpl by mapstruct没有生成getter和setter。

支才
2023-03-14

我想知道为什么我的映射器由映射结构生成的映射器不是用获取器和设置器生成的。

我认为正因为如此,当我去对邮递员发出GET请求时,它会返回所有不应该返回空值的空值。

我使用专家,java 8和Spring启动

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 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.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.celulaweb</groupId>
    <artifactId>crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>crud</name>
    <description>Crud Estudo CelulaWeb</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.1.Final</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <outputChangeLogFile>src/main/resources/liquibase-outputChangeLog.xml</outputChangeLogFile>
                    <url>jdbc:mysql://localhost/crud</url>
                    <username>root</username>
                    <password>root</password>
                </configuration>
            </plugin>
            <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.4.1.Final</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

映射器:

package com.celulaweb.crud.service.mappers;

import com.celulaweb.crud.domain.Cidade;
import com.celulaweb.crud.service.DTO.CidadeDTO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper(componentModel = "spring")
public interface CidadeMapper {

    CidadeDTO cidadeParaDTO(Cidade cidade);
    List<CidadeDTO> cidadeParaDTO(List<Cidade> cidade);
    Cidade cidadeDTOParaCidade(CidadeDTO cidadeDTO);

}

实体DTO:

package com.celulaweb.crud.service.DTO;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class CidadeDTO implements Serializable {

    private Long id;
    private String nome;
    private Integer qtdHabitantes;
    private String estado;

}

实体:

package com.celulaweb.crud.domain;

import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;

@Entity
@Table(name = "cidade")
@Getter
@Setter
public class Cidade {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotNull
    @Size(min = 3, max = 100)
    private String nome;

    private Integer qtdHabitantes;

    @NotNull
    private String estado;

    @Override
    public boolean equals(Object obj) {
        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) {
            return false;
        }
        Cidade rhs = (Cidade) obj;
        return new EqualsBuilder()
                .appendSuper(super.equals(obj))
                .append(id, rhs.id)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).toHashCode();
    }
}

映射器实现:

package com.celulaweb.crud.service.mappers;

import com.celulaweb.crud.domain.Cidade;
import com.celulaweb.crud.service.DTO.CidadeDTO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2022-01-20T13:49:14-0300",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.13 (Ubuntu)"
)
@Component
public class CidadeMapperImpl implements CidadeMapper {

    @Override
    public CidadeDTO cidadeParaDTO(Cidade cidade) {
        if ( cidade == null ) {
            return null;
        }

        CidadeDTO cidadeDTO = new CidadeDTO();

        return cidadeDTO;
    }

    @Override
    public List<CidadeDTO> cidadeParaDTO(List<Cidade> cidade) {
        if ( cidade == null ) {
            return null;
        }

        List<CidadeDTO> list = new ArrayList<CidadeDTO>( cidade.size() );
        for ( Cidade cidade1 : cidade ) {
            list.add( cidadeParaDTO( cidade1 ) );
        }

        return list;
    }

    @Override
    public Cidade cidadeDTOParaCidade(CidadeDTO cidadeDTO) {
        if ( cidadeDTO == null ) {
            return null;
        }

        Cidade cidade = new Cidade();

        return cidade;
    }
}

如果有人知道怎么解决,请帮忙。谢谢;)

共有1个答案

满耀
2023-03-14

如果你想和lombok一起使用mapstruct,你必须在你的pom.xml中包含lombok-mapstruct绑定

看看“我能把MapStruct和Project Lombok一起用吗?”在https://mapstruct.org/faq/的统治下

喜欢这个:

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </path> 
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-mapstruct-binding</artifactId>
        <version>0.2.0</version>
    </path>
    <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.4.1.Final</version>
    </path>
</annotationProcessorPaths>
 类似资料:
  • 我在为Lombok getter和setter生成Javadoc时遇到问题。这两个建议我都试过了。示例字段及其文本如下所示: 但是,我在生成的Javadoc中既没有看到getter,也没有看到setter。我在Eclipse上使用受保护的可见性(项目-

  • 我只是试图将一个基于Maven的项目发送到另一台计算机上,而可怕的是,到处都是红色标记!! 但是,构建得很好。 很快,我注意到Lombok没有为我的类生成getter和setter,尽管和被Eclipse正确识别。

  • e(fx)clipse插件为Eclipse提供了一个generate命令,该命令将生成如上所述的JavaFX getter/setter。IntelliJ IDEA有类似的功能吗?

  • 菜单栏: Code —> Generate —> Getter and Setter Mac: command + N Windows\/Linux: Alt + Insert —> Getter and Setter

  • 问题内容: 我正在开发一个新项目(),并创建了一个包含大量变量的Object。由于我打算为所有这些添加吸气剂和设置器,所以我想知道:是否存在在给定类中自动生成吸气剂和设置器的捷径? 问题答案: 在所需类的源代码窗口中弹出上下文菜单(即右键单击)。然后选择子菜单;从该菜单中进行选择将导致出现向导窗口。 选择您要为其创建getter和setter的变量,然后单击。