我正在使用Spring和MySQL开发一个新的应用程序。我习惯于通过Spring工具套件使用Spring。然而,对于这个项目,我决定使用IntelliJ。我做了经典的,通过start.spring.io创建项目
<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.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kevi</groupId>
<artifactId>PortFolio</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PortFolio</name>
<description>Presentation of my compétences</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
字符串
我配置了application.properties文件
server.port=7070
#jdbc properties
spring.datasource.url=jdbc:mysql://localhost:3306/portfolioDatabase?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=zizou123
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#hibernate properties
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
型
我创建了用户实体。
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name="users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idUser;
private String lastNameUser;
private String firstNameUser;
public User() {
}
public User(String lastNameUser, String firstNameUser) {
this.lastNameUser = lastNameUser;
this.firstNameUser = firstNameUser;
}
public String getLastNameUser() {
return lastNameUser;
}
public String getFirstNameUser() {
return firstNameUser;
}
public void setLastNameUser(String lastNameUser) {
this.lastNameUser = lastNameUser;
}
public void setFirstNameUser(String firstNameUser) {
this.firstNameUser = firstNameUser;
}
}
型
应用程序正确启动,创建了数据库,但没有创建用户表。我不明白为什么,有人能帮助我吗?
提前感谢您的帮助!
附件:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PortFolioApplication {
public static void main(String[] args) {
SpringApplication.run(PortFolioApplication.class, args);
}
}```
型
1条答案
按热度按时间hs1rzwqc1#
在项目中,“PortFolioApplication.java"在com.kevi.PortFolio包中,而”User.java"在com.kevin.PortFolio.entities包中。
这里,“*PortFolioApplication.java 包”的包名"kevi"不同。因此,将其更改为“kevin”并运行代码。它将工作。
Sping Boot @SpringBootApplication annotation也会从它的包触发组件扫描,组件扫描也会扫描包com.kevin.PortFolio.entities。