【问题标题】:Not able to add JPA dependency into spring-boot project无法将 JPA 依赖项添加到 spring-boot 项目中
【发布时间】:2018-08-24 11:05:44
【问题描述】:

我正在尝试将 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>

application.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 starter project > 并立即添加 JPA、Web 和 H2 但仍然相同的错误。

我正在使用 STS IDE,可能与它有关吗?

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa


    【解决方案1】:
     <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/

    【讨论】:

      【解决方案2】:

      在仅使用 Web 创建 Spring boot 后,我​​尝试在 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 步 File -> Invalidate Caches / Restart... 并选择 Invalidate and Restart 选项解决了这个问题。可能是过时的缓存问题

      第 2 步 在 IntelliJ Idea 下打开 POM.xml 右键单击​​-> Maven-> 重新加载项目。 Maven 将下载 JPA 依赖 jar 并将添加到项目中。验证是否添加了 JPA jar。

      第 3 步 选择 File -> Invalidate Caches / Restart... 并选择 Invalidate and Restart 选项解决了这个问题。

      注意:- 如果您没有使用 ItellIj id,请强制 Maven 下载依赖项(用于第 2 步)

      【讨论】:

        猜你喜欢
        • 2017-03-19
        • 1970-01-01
        • 2019-07-28
        • 2015-12-28
        • 1970-01-01
        • 2022-01-01
        • 2017-05-29
        • 2019-02-09
        • 1970-01-01
        相关资源
        最近更新 更多