Intellij Idea intelliJ spring错误没有创建表用户

sh7euo9m  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(278)

我正在使用Spring和MySQL开发一个新的应用程序。我习惯于通过Spring工具套件使用Spring。然而,对于这个项目,我决定使用IntelliJ。我做了经典的,通过start.spring.io创建项目

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>3.1.5</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.kevi</groupId>
  11. <artifactId>PortFolio</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>PortFolio</name>
  14. <description>Presentation of my compétences</description>
  15. <properties>
  16. <java.version>17</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-data-jpa</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.security</groupId>
  25. <artifactId>spring-security-core</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.mysql</groupId>
  33. <artifactId>mysql-connector-j</artifactId>
  34. <scope>runtime</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.security</groupId>
  43. <artifactId>spring-security-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. <configuration>
  53. <image>
  54. <builder>paketobuildpacks/builder-jammy-base:latest</builder>
  55. </image>
  56. </configuration>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

字符串
我配置了application.properties文件

  1. server.port=7070
  2. #jdbc properties
  3. spring.datasource.url=jdbc:mysql://localhost:3306/portfolioDatabase?createDatabaseIfNotExist=true
  4. spring.datasource.username=root
  5. spring.datasource.password=zizou123
  6. spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  7. #hibernate properties
  8. spring.jpa.hibernate.ddl-auto=create
  9. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect


我创建了用户实体。

  1. import jakarta.persistence.Entity;
  2. import jakarta.persistence.GeneratedValue;
  3. import jakarta.persistence.GenerationType;
  4. import jakarta.persistence.Id;
  5. import jakarta.persistence.Table;
  6. import java.io.Serializable;
  7. @Entity
  8. @Table(name="users")
  9. public class User implements Serializable {
  10. @Id
  11. @GeneratedValue(strategy = GenerationType.IDENTITY)
  12. private Long idUser;
  13. private String lastNameUser;
  14. private String firstNameUser;
  15. public User() {
  16. }
  17. public User(String lastNameUser, String firstNameUser) {
  18. this.lastNameUser = lastNameUser;
  19. this.firstNameUser = firstNameUser;
  20. }
  21. public String getLastNameUser() {
  22. return lastNameUser;
  23. }
  24. public String getFirstNameUser() {
  25. return firstNameUser;
  26. }
  27. public void setLastNameUser(String lastNameUser) {
  28. this.lastNameUser = lastNameUser;
  29. }
  30. public void setFirstNameUser(String firstNameUser) {
  31. this.firstNameUser = firstNameUser;
  32. }
  33. }


应用程序正确启动,创建了数据库,但没有创建用户表。我不明白为什么,有人能帮助我吗?
提前感谢您的帮助!
附件:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class PortFolioApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(PortFolioApplication.class, args);
  7. }
  8. }```

hs1rzwqc

hs1rzwqc1#

在项目中,“PortFolioApplication.java"在com.kevi.PortFolio包中,而”User.java"在com.kevin.PortFolio.entities包中。
这里,“*PortFolioApplication.java 包”的包名"kevi"不同。因此,将其更改为“kevin”并运行代码。它将工作。
Sping Boot @SpringBootApplication annotation也会从它的包触发组件扫描,组件扫描也会扫描包com.kevin.PortFolio.entities

相关问题