簡體   English   中英

無法將 JPA 依賴項添加到 spring-boot 項目中

[英]Not able to add JPA dependency into spring-boot project

我正在嘗試將 JPA Maven 依賴項添加到已創建的 spring-boot 項目中,但出現以下錯誤:

Error: Could not find or load main class com.kame.demo.DemoApplication

當我刪除它時,錯誤消失了。

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>8.5.32</version>
        </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-data-jpa</artifactId>
        </dependency>
    </dependencies>

應用程序.properties

spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:navin

我試圖在網上找到答案,但沒有一個適合我。

還嘗試創建 > Spring 啟動項目 > 並立即添加 JPA、Web 和 H2,但仍然出現相同的錯誤。

我正在使用 STS IDE,可能與它有關嗎?

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>

本身就是一個演示項目,有一個擴展的 SpringBoot 類作為你項目的main類。 但是這種依賴性是一樣的:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

所以兩者很可能會發生沖突......解決方案是為 jpa 導入正確的依賴項而不是 spring boot starter jpa 依賴項。

編輯

這個可能會起作用:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
  </dependency>

但我建議您閱讀官方文檔以正確入門: https ://docs.spring.io/spring-data/jpa/docs/2.1.0.RC2/reference/html/

在僅使用 Web 創建 Spring 引導后,我嘗試在 POM.xml 中手動添加 JPA 依賴項時遇到了同樣的問題。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <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>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

問題的根本原因:- 缺少 JPA 依賴項 jar。 驗證:- File->Project structure->Modules->Dependencies 並且您不會找到任何 jar org.springframework.boot:spring-boot-starter-data-jpa :2.4.3(我使用的是 spring boot 2.4.3 但是在你的情況下版本可能不同)

解決方案:-第 1 步文件 -> 使緩存無效/重新啟動...並選擇無效並重新啟動選項解決了該問題。 可能是陳舊的緩存問題

第 2 步在 IntelliJ Idea 下打開POM.xml 右鍵單擊 -> Maven ->重新加載項目 Maven 將下載 JPA 依賴 jar 並將添加到項目中。 驗證是否添加了 JPA jar。

第 3 步選擇 File -> Invalidate Caches / Restart... 並選擇 Invalidate and Restart 選項解決了這個問題。

注意:- 如果您沒有使用 ItellIj id,那么請強制 Maven 下載依賴項(對於第 2 步)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM