【问题标题】:Spring boot - MySQL settings are not working春季启动 - MySQL 设置不起作用
【发布时间】:2023-12-09 23:55:01
【问题描述】:

我正在尝试使用 Spring Boot 和 MySQL 开发应用程序。正如文档所说,首先我使用 Intelij Idea 使用 Spring initializr 创建了项目,配置了application.properties 文件,并编写了schema-mysql.sql 文件和data-mysql.sql 文件。运行项目后,发现 MySQL 数据库中没有表或数据。我的配置有什么问题?请帮忙。

application.properties 文件,

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password

spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql

pom.xml 文件中的依赖项,

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

schema-mysql.sql 文件,

CREATE TABLE IF NOT EXISTS `SOL16_USERS` (
  `USERNAME` VARCHAR(200) NOT NULL,
  'PASSWORD' VARCHAR(200) NOT NULL,
  PRIMARY KEY (`USERNAME`)
);

CREATE TABLE IF NOT EXISTS 'SOL16_PRIVILEGES' (
  'PRIVILEGE_ID' INT(2)      NOT NULL,
  'PRIVILEGE'    VARCHAR(15) NOT NULL,
  PRIMARY KEY ('PRIVILEGE_ID')
);

CREATE TABLE IF NOT EXISTS 'SOL16_USER_PRIVILEGES' (
  'USERNAME'     VARCHAR(200) NOT NULL,
  'PRIVILEGE_ID' VARCHAR(2)   NOT NULL,
  PRIMARY KEY ('USERNAME')
);

而文件/目录结构是,

src
|----main
|    |----java
|    |----resources
|    |    |----static
|    |    |----templates
|    |    |----application.properties
|    |    |----data-mysql.sql
|    |    |----schema-mysql.sql

【问题讨论】:

  • 你配置了application.yml文件吗?
  • 您是否在应用程序运行时进行检查? create-drop 会在会话关闭后删除表格 - 尝试使用 create 或更好的 update
  • 你遇到了什么错误?
  • 您的schema-mysql.sqldata-mysql.sql 放在哪里(请将您的文件夹结构添加到您的问题中)?此外,您正在使用 Hibernate create-drop,这会干扰 Spring Boot 的模式创建。假设您只想使用 schema-mysql.sql 而不是依赖 Hibernate 将其更改为 validate 而不是您现在拥有的。
  • validate 不会出错???如果没有表,那应该会炸毁您的应用程序...为 .sql 文件的名称添加前缀 classpath: 以使它们从类路径中显式加载。您创建的表是映射到休眠实体的表?

标签: java mysql spring intellij-idea spring-boot


【解决方案1】:

正如 Spring boot documentation 所提到的,解决我的问题是将 spring.datasource.platform 添加到 application.properties。这就是我使用schema-{platform}.sqldata-{platform}.sql 初始化架构时所缺少的。

{platform} = spring.datasource.platform的值

所以我最终的 application.properties 文件是,

spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username = root
spring.datasource.password = password

spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.datasource.platform=mysql
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
spring.datasource.initialize=true
spring.datasource.continue-on-error=true

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并在 spring boot 2.0 中应用以下代码解决了它

    spring.datasource.initialization-mode=always

    【讨论】:

      【解决方案3】:

      您需要在 application.properties 中添加以下代码 并打开您的 xammp/wamp 或服务器,然后创建您的数据库,例如 studentdb 在 application.properties 文件中给出相同的名称

      spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
      spring.datasource.username=root
      spring.datasource.password=   
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
      spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
      spring.jpa.generate-ddl=true
      spring.jpa.hibernate.ddl-auto = update
      

      【讨论】:

        【解决方案4】:

        请使用下面给出的关键字段---->

        spring.jpa.hibernate.ddl-auto=create 
        

        spring.jpa.hibernate.ddl-auto=update
        

        【讨论】: