在Spring Boot中创建表并从.sql文件导入数据

施罗丁格

目前的方法:

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文件?

帕特尔·罗米尔(Patel Romil)

更新

我们可以用

spring.datasource.schema= # Schema (DDL) script resource references.
spring.datasource.data= # Data (DML) script resource references.
  • 无需更改SQL文件名
  • 可以将模式生成和插入保持在同一文件中
  • 可以指定多个文件

    spring.datasource.schema = classpath:/abc.sql,classpath:/abc2.sql

注意:

  • 为了不使用模式生成和在同一文件中插入spring.datasource.data,我们必须使用spring.datasource.schema
  • 将所有文件保留在src / main / resources中
  • 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Java Spring在SQL Server中创建动态表

在Spring Boot应用程序中无法从data.sql导入sql插入脚本数据

在Spring Boot中创建数据库表后运行自定义脚本

在Spring XML中通过EL导入spring配置文件

在表中实现一个按钮,使用Thymeleaf和Spring Boot从表中返回数据

使用Spring Boot导入数据

将多个分隔的文本文件导入到SQL Server数据库中并自动创建表

如何通过Spring Boot Web App快速创建mysql数据库和表

如何在Spring Boot + Spring Data中创建自定义数据源

Spring Boot JPA如何从ManyToMany表中检索数据?

如何使用Spring Boot将JSON数据文件导入mongodb?

Spring Boot不映射MS SQL中的存在表

在表上创建视图并从表中获取所有数据时,对性能的影响

Spring Boot中的多个SQL导入文件

将数据从文本文件导入sql表

如何在Spring Boot中创建日志文件?

Spring Boot - Hibernate - 从 SQL 查询导入日期

Spring Boot 不会在 Postgres 中创建表

Spring Boot 不从 data.sql 文件插入数据?

表未在 Spring Boot H2 中创建

data.sql 文件未在 Spring Boot 中将数据泵送到表中

Spring Boot h2 初始数据不先创建表

创建表并从 CSV 文件复制数据

Spring boot 运行控制台说表已创建,但该表不存在于数据库中

如何使用spring使用data.sql文件创建表?

在 spring boot 中过滤数据

如何在 Spring Boot 项目目录中创建日志文件?

无法使用 Spring Boot 和 JPA 在数据库中创建表

无法使用 Spring Boot 和 JPA 在数据库中创建表