java - 无法将 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,可能与它有关吗?
解决方案
<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/
推荐阅读
- objective-c - 带有无效数据库连接指针的目标 C-sqlite3 API 调用,在第 162918 行滥用
- javascript - Nodejs await 仅在 Promise 循环中的异步函数中有效
- html - 否 - 带有可扩展中间部分的滚动页面
- javascript - 为什么在 FF 中播放音频。但我使用暂停()?
- flutter - Flutter Inspector 选择小部件模式键盘快捷键?
- javascript - 不能在 if 和 else 中使用 preventDefault()
- python - Python - 一次多次运行部分代码
- python - Python:压缩嵌套数组
- java - `allOf` 的通用版本,导致通用列表的 `ComposableFuture`
- c# - 从数据库中填充 datagridview