首页 > 解决方案 > 无法将 JPA 依赖项添加到 spring-boot 项目中

问题描述

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

应用程序属性

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,可能与它有关吗?

标签: javaspringspring-bootspring-data-jpa

解决方案


 <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/


推荐阅读