java Lombok在Sping Boot 项目中没有生成getter和setter或构造函数,甚至@Data

j1dl9f46  于 2023-11-15  发布在  Java
关注(0)|答案(5)|浏览(110)

我使用Sping Boot 初始化我的项目,实际上,我通过Sping Boot 包含了Lombok依赖项,并如下配置了Lombok插件:
我使用的IDE是Eclipse

<?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>3.2.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.springbootExample</groupId>
    <artifactId>SpringBoot_Example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot_Example</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>17</java.version>
    </properties>
    
    <dependencies>
        
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</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>
        
        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>12.4.2.jre11</version>
        </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>
  </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>
  
  <repositories>
      
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
    
  </repositories>
  
  <pluginRepositories>
      
    <pluginRepository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
    
    <pluginRepository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </pluginRepository>
    
  </pluginRepositories>

</project>

字符串
然而,当我在名为department.java的实体类中使用它时,Lombok似乎没有按预期工作。即使使用@Data,@NoArgsConstructor,@AllArgsConstructor和@Builder注解该类后,问题仍然存在。

package com.springbootExample.SpringBoot_Example.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class department {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long departmentID;
    
    @NotBlank(message = "Please add department name")
//  @Length(max = 20, min = 10)
//  @Size
//  @Email
//  @Positive
//  @Negative
//  @PositiveOrZero
//  @Future
//  @Past
//  @FutureOrPresent
    private String departmentName;
    private String departmentAdress;
    private String departmentCode;
}


在另一个配置Java类中,我也无法使用department类的getter和setter方法。

package com.springbootExample.SpringBoot_Example.service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

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

import com.springbootExample.SpringBoot_Example.entity.department;
import com.springbootExample.SpringBoot_Example.error.departmentExceptionHandling;
import com.springbootExample.SpringBoot_Example.repository.departmentRepository;

@Service
public class departmentServiceImplement implements departmentService{
    
    @Autowired
    private departmentRepository dr;

    @Override
    public department saveDepartment(department dp) {
        return dr.save(dp);
    }

    @Override
    public List<department> fetchDepartment() {
        return dr.findAll();
    }

    @Override
    public department fetchDepartmentById(Long departmentID) throws departmentExceptionHandling {
        Optional<department> dpt = dr.findById(departmentID);
        
        if(!dpt.isPresent()) {
            throw new departmentExceptionHandling("Department was not found");
        }
        return dpt.get();
    }

    @Override
    public void deleteDepartmentById(Long departmentID) {
        dr.deleteById(departmentID);
        
    }

    @Override
    public department updateDepartment(Long departmentID, department dp) {
        department dep = dr.findById(departmentID).get();
        
        if(Objects.nonNull(dp.getDepartmentName()) && !"".equalsIgnoreCase(dp.getDepartmentName())) {
            dep.setDepartmentName(dp.getDepartmentName());
        }
        
        if(Objects.nonNull(dp.getDepartmentCode()) && !"".equalsIgnoreCase(dp.getDepartmentCode())) {
            dep.setDepartmentCode(dp.getDepartmentCode());
        }
        
        if(Objects.nonNull(dp.getDepartmentAdress()) && !"".equalsIgnoreCase(dp.getDepartmentAdress())) {
            dep.setDepartmentAdress(dp.getDepartmentAdress());
        }
        
        return dr.save(dep);
    }

    @Override
    public department fetchDepartmentByName(String departmentName) {
        return departmentRepository.findByDepartmentNameIgnoreCase(departmentName);
    } 

}


错误显示为

The method getDepartmentName() is undefined for the type department
The method getDepartmentCode() is undefined for the type department
The method getDepartmentAdress() is undefined for the type department


you guy can see the image below
我尝试了几种解决方案,例如更新IDE,更改Lombok插件和安装Lombok,但没有一个解决了这个问题。
如果有人遇到类似的问题,并知道如何解决它,我将非常感谢您的帮助。
先谢谢你了。

zy1mlcev

zy1mlcev1#

尝试删除目标文件夹并再次运行您的项目。它与我一起工作

jvidinwx

jvidinwx2#

尝试在IDE中安装项目lombok..然后只有该依赖项按预期工作。项目lombok下载链接:https://projectlombok.org/download ..下载此lombok然后在选择IDE后打开文件然后单击安装..安装lombok后..关闭IDE然后打开IDE

7qhs6swi

7qhs6swi3#

经过几天的故障排除,我终于找到了我的项目或Eclipse IDE无法使用Lombok的原因。为了帮助那些可能面临类似Lombok问题的人,我在这里分享解决方案作为答案。
所以首先你得

Help --> About Eclipse IDE

if you have install Lombok plugin you will see this line --> Lombok v1.18.30 "Envious Ferret" is installed. https://projectlombok.org/

if don't have or already installed but Lombok not work then you go to Installation Detail --> search on the searching bar find the Lombok then delete it

字符串
然后你把这个URL

https://projectlombok.org/p2

go to --> help (section in eclipse) --> install new software --> you will see the "work with" --> pass the URL "https://projectlombok.org/p2" --> click add --> set name is Lombok --> click add

after that tick to the Lombok checkbox then click next --> finish --> wait for the installment completed --> restart the eclipse

then you can use Lombok already


下面这里是图像的描述,你可以看到通过以下步骤
add Lombok with then URL I use above to install Lombok
click finish to install Lombok
非常感谢大家,希望我的问题和解决方法对大家有帮助

ckocjqey

ckocjqey4#

我在Eclipse上工作时也遇到了同样的问题。即使在pom.xml文件中添加jar作为Maven依赖项之后,Eclipse仍然无法识别jar。但是,我能够通过以下步骤解决它:

  • 确保将jar添加到pom.xml文件后,它在.m2文件夹中可用。
  • 打开命令提示符并导航到jar的位置。
  • 运行以下命令:java -jar lombok.jar。将名称替换为准确的版本
  • 将出现Lombok安装窗口。
  • 指定Eclipse安装目录的位置。这将把jar复制到同一个文件夹,并使用必要的条目更新eclipse.ini文件。这一步应该可以解决Lombok jar问题。
  • 要确认安装,请转到Eclipse,单击“Help”,然后选择“About IDE”。滚动到页面末尾,您应该会看到Lombok的安装确认。
13z8s7eq

13z8s7eq5#

这些注解是要在编译前执行的,在Gradle中需要使用annotationProcessor <lombok>来实现。您添加的是默认作用域,可能是runtimecompile(不确定)。
对于Maven,您将不得不使用annotationProcessor的等价物,这是here(在问题本身中)。

相关问题