Spring启动 - MySQL设置不起作用

时间:2016-10-25 18:37:06

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

我正在尝试使用Spring启动和MySQL开发应用程序。正如文档所述,首先我使用Spring initializr使用Intelij Idea创建项目,配置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

4 个答案:

答案 0 :(得分:1)

正如Spring 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

答案 1 :(得分:1)

我遇到了同样的问题,并在Spring Boot 2.0中应用以下代码解决了该问题

  

spring.datasource.initialization-mode =始终

答案 2 :(得分:0)

您需要在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

答案 3 :(得分:0)

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

spring.jpa.hibernate.ddl-auto=create 

spring.jpa.hibernate.ddl-auto=update