【问题标题】:No tables created in the MySQL database in Spring Boot application在 Spring Boot 应用程序的 MySQL 数据库中没有创建表
【发布时间】:2017-05-07 07:00:22
【问题描述】:

这是我的 Application.java:

@Configuration
@EnableAutoConfiguration(exclude =
    {JpaRepositoriesAutoConfiguration.class})
@ComponentScan
@SpringBootApplication
public class UserFrontApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserFrontApplication.class, args);
    }
}

application.properties 的内容:

spring.datasource.driverClassName=org.gjt.mm.mysql.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

pom.xml:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.userFront</groupId>
<artifactId>userfront</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>UserFront</name>
<description>User frontend for online banking</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.5.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.3.Final</version>
    </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>

应用程序运行良好,但没有创建表。

必须存储在数据库中的用户类:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "userId", nullable = false, updatable = false)
    private Long userId;
}

【问题讨论】:

  • 您是否得到任何异常或堆栈跟踪。假设您看一下示例项目here
  • 你的 User 类所在的包是什么?

标签: java mysql spring spring-boot


【解决方案1】:

为什么你使用 org.hibernate.dialect.PostgreSQLDialect。您需要根据您的数据库更新方言,更新这两个属性。

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2021-01-09
    • 2014-12-27
    • 1970-01-01
    • 2021-03-04
    • 2015-11-11
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多