我正在尝试使用Spring Boot和MySQL开发应用程序。正如文档所述,首先,我使用Intelij Idea使用Spring initializr创建了项目,配置了application.properties文件,并编写了schema-mysql.sql文件和data- mysql.sql文件。运行项目后,我发现MySQL数据库中没有表或数据。我的配置有什么问题?请帮忙。
application.properties
schema-mysql.sql
data- mysql.sql
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
正如Spring引导文档中提到的那样,解决我的问题的方法是添加spring.datasource.platform到application.properties。这就是我缺少使用schema-{platform}.sql和初始化架构的原因data-{platform}.sql。
spring.datasource.platform
schema-{platform}.sql
data-{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