所以我花了几个小时来解决这个问题,这真的很烦人。我用Spring Initializr生成了一个Sping Boot 项目:java 19,spring 3.0.0,maven,with mariadb driver,spring web,spring data jpa dependencies.问题是我创建了一个模型类Student后,无法从javax. persistence导入@Entity注解.我看的所有教程和视频,他们是从javax.persistence导入的,但是我只得到了jakarta. persistence。为什么?我试图为javax添加自己的依赖项。持久性,但是在我创建了带有所有注解(@Entity、@Table、@column)的模型后,表没有创建。
这里是我的文件:
pom.xml
<?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.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
</project>
Student.class
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity(name = "student") // CAN'T GET FROM JAVAX.PERSISTENCE, SAME FOR ANNOTATIONS BELOW
public class Student {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
}
application.properties 位于src/main/resources中
spring.datasource.url=jdbc:mariadb://localhost:3307/db1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
入口点
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我做错了什么?
1条答案
按热度按时间rkkpypqq1#
在最新版本的Sping Boot 中,
javax
库被替换为jakarta
(更多详细信息请参见此处https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M1-Release-Notes)和此处https://spring.io/blog/2022/01/20/spring-boot-3-0-0-m1-is-now-available,因此您可以只找到jakarta
。您可以继续使用它。