运行项目后,用户表不应该在我的数据库中自动生成吗?我也在运行服务器后刷新它,但是用户表没有显示在mysql中。
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/BloggingApp
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
用户类
package com.example.entities;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String email;
private String password;
private String about;
}
主要
package com.example.blogging.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties
@EntityScan
public class BloggingAppApplication {
public static void main(String[] args) {
SpringApplication.run(BloggingAppApplication.class, args);
}
}
项目结构:
由于您的实体包不同于Spring boot main类,因此您必须在@EntityScan
中指定包
@EntityScan("com.example.entities.*")