简体   繁体   English

Spring Boot - MySQL 设置不起作用

[英]Spring boot - MySQL settings are not working

I am trying to develop an application using Spring boot and MySQL.我正在尝试使用 Spring Boot 和 MySQL 开发应用程序。 As the documentation said, First I created the project using Spring initializr using Intelij Idea, configured the application.properties file, and wrote schema-mysql.sql file and data-mysql.sql file.正如文档所说,首先我使用Intelij Idea使用Spring initializr创建了项目,配置了application.properties文件,并编写了schema-mysql.sql文件和data-mysql.sql文件。 After I ran the project, I found there are no tables or data in the MySQL database.跑完项目,发现MySQL数据库中没有表,也没有数据。 What is wrong with my configuration?我的配置有什么问题? Please help.请帮忙。

application.properties file, 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

dependencies in pom.xml file, 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 file, 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')
);

And the file/directory structure is,文件/目录结构是,

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

As the Spring boot documentation mentioned, what have fixed my issue was adding spring.datasource.platform to the application.properties .正如 Spring 启动文档所提到的,解决我的问题是将spring.datasource.platform添加到application.properties That is what I have been missing to initialize the schema using schema-{platform}.sql and data-{platform}.sql .这就是我使用schema-{platform}.sqldata-{platform}.sql初始化架构时所缺少的。

{platform} = value of spring.datasource.platform {platform} = spring.datasource.platform

So my final application.properties file is,所以我的最终 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

I faced the same issue and resolved it applying the below code in spring boot 2.0我遇到了同样的问题并在 spring boot 2.0 中应用以下代码解决了它

spring.datasource.initialization-mode=always spring.datasource.initialization-mode=always

You need to add below code in application.properties and open your xammp/wamp or server then create yourdatabase for example studentdb same name give in application.properties file您需要在 application.properties 中添加以下代码并打开您的 xammp/wamp 或服务器,然后创建您的数据库,例如在 application.properties 文件中给出相同名称的 studentdb

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

Please use the key field given below---->请使用下面给出的关键字段---->

spring.jpa.hibernate.ddl-auto=create 

or

spring.jpa.hibernate.ddl-auto=update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM