目前的方法:
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.connection.zeroDateTimeBehavior=convertToNull
spring.datasource.initialization-mode=always
spring.jpa.properties.hibernate.hbm2ddl.import_files=<name>.sql
spring.datasource.platform=mysql
不确定我缺少什么,为什么在此配置中不执行.sql文件?
更新
我们可以用
spring.datasource.schema= # Schema (DDL) script resource references.
spring.datasource.data= # Data (DML) script resource references.
可以指定多个文件
spring.datasource.schema = classpath:/abc.sql,classpath:/abc2.sql
注意:
spring.datasource.data
,我们必须使用spring.datasource.schema
spring.jpa.hibernate.ddl-auto=none
初步答案
Spring boot已经配置了Hibernate以基于您的实体创建架构。要使用SQL(在src / main / resources)文件集中创建它
spring.jpa.hibernate.ddl-auto=none
在src / main / resources中创建schema.sql(用于创建表)和data.sql(用于插入记录)
schema.sql
CREATE TABLE country (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
PRIMARY KEY (id)
);
data.sql
INSERT INTO country (name) VALUES ('India');
INSERT INTO country (name) VALUES ('Brazil');
INSERT INTO country (name) VALUES ('USA');
INSERT INTO country (name) VALUES ('Italy');
application.properties
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句