hibernate 未在SpringBoot应用程序中创建数据库和表

brgchamk  于 2022-11-14  发布在  Spring
关注(0)|答案(2)|浏览(129)

我已经检查了所有依赖项,并添加了SQL驱动程序。我正在使用MySQL数据库。在运行Spring Boot应用程序之后,不会创建数据库。我正在使用STS4中的数据管理工具。
我希望创建CitizenDB数据库和数据库表Citizen,因为我已经向application.yml配置文件添加了所有必需的属性
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>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>CitizenService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CitizenService</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>16</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client4</artifactId>
            <version>1.19.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

应用程序.yml:

server:
  port: 8081
    
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/CitizenDB
    username: root
    password: March@031996
    driver-class-name: com.mysql.cj.jdbc.Driver  
  application:
    name: CITIZEN-SERVICE
    jpa:
     show-sql: true
     database-platform: org.hibernate.dialect.MYSQLInnoDBDialect
     generate-ddl: true  
     hibernate:
       ddl-auto: update

实体类:

package com.example.demo.CitizenService.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Citizen {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column
    private String name;
    @Column
    private int vaccinationId;
}

主班:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class CitizenServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(CitizenServiceApplication.class, args);
    }
}
egmofgnx

egmofgnx1#

首先,请在您的问题中包含格式良好的代码,这些代码可以很容易地复制粘贴以重现您的问题。
特别是对不同的文件和文件类型使用不同的代码块。您的application.yml配置完全不可读。
删除所有文本行,如enter code here package com.example.demo.CitizenService;ENTITY CLASS:。它们会使您的代码无法编译。改为使用注解和javadoc。但是,如果您不想这样做,您可以添加标志
在您的IDE中使用代码格式。IntelliJ Idea的例子。
第二,请明确说明您的问题以及您为解决该问题所做的具体工作。我假设您预期您的Spring Boot应用程序应该在其中创建数据库CitizenDBCitizen表。
默认情况下,Spring(更具体地说是Hibernate)不会创建数据库,这是一种糟糕的做法。它假定您已经创建了一个数据库,最好是自己创建。但是,如果您仍然希望自动创建数据库,则可以在数据源URL中指定这样的标志createDatabaseIfNotExist=true。您的案例示例:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/CitizenDB?createDatabaseIfNotExist=true

您必须改进对DataSourceAutoConfiguration类的排除,因为您希望Spring为您配置所有内容。您的主类应该如下所示:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CitizenServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(CitizenServiceApplication.class, args);
    }
}
n1bvdmb6

n1bvdmb62#

只需添加createDatabaseIfNotExist=true,如下所示

spring
    datasource
        url=jdbc:mysql://localhost:3306/CitizenDB?createDatabaseIfNotExist=true

添加到您的应用程序.yml文件中

相关问题