通过在 Spring Batch 中调用 .sql 文件自动创建表? [英] Create tables automatically by calling .sql file in Spring Batch?

查看:118
本文介绍了通过在 Spring Batch 中调用 .sql 文件自动创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了许多链接,例如

application.properties

# 数据源spring.datasource.url=jdbc:mysql://localhost:3306/spring_batchspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.username=rootspring.datasource.password=rootspring.jpa.hibernate.ddl-auto=更新# SPRING BATCH (BatchProperties)# 数据库模式初始化模式.spring.batch.initialize-schema=总是# 在启动时执行上下文中的所有 Spring Batch 作业.#spring.batch.job.enabled=false# 用于初始化数据库架构的 SQL 文件的路径.spring.batch.schema=classpath:schema-all.sql

解决方案

我观察到的是 -

如果我在下面启用

<块引用>

spring.batch.initialize-schema=always

然后所有的表都创建得很好

 +------------------------------+|表_in_test |+------------------------------+|batch_job_execution ||batch_job_execution_context ||batch_job_execution_params ||batch_job_execution_seq ||batch_job_instance ||batch_job_seq ||batch_step_execution ||batch_step_execution_context ||batch_step_execution_seq |

  1. 如果我把下面这行,那么它只会创建report.xml<块引用>

    spring.batch.schema=classpath:schema-all.sql

如果我们可以同时创建,有什么办法吗?

I followed many links like Spring Batch Framework - Auto create Batch Table and https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/#howto-initialize-a-spring-batch-database, but that doesn't satisfy my requirement

In Spring Boot 2.0, how we can call sql file (created manually and have all the schema details) so that Spring Batch can create it manually for us and if schema is already present then skip the execution ?

<!-- Create meta-tables -->
<jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:hsqldb/initial-query.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-drop-hsqldb.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-hsqldb.sql" />
 </jdbc:initialize-database>

I am looking to do 1. If Schema already present then don't create any tables 2. If schema is not present then create the tables

Project structure

DROP TABLE IF EXISTS report;

CREATE TABLE report  (
    id INT NOT NULL PRIMARY KEY,
    date DATETIME,
    impression BIGINT,
    clicks INT,
    earning DECIMAL(12,4)
  )ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

application.properties

# Data Source
spring.datasource.url=jdbc:mysql://localhost:3306/spring_batch
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update


# SPRING BATCH (BatchProperties)
# Database schema initialization mode.
spring.batch.initialize-schema=always

# Execute all Spring Batch jobs in the context on startup.
#spring.batch.job.enabled=false

# Path to the SQL file to use to initialize the database schema.
spring.batch.schema=classpath:schema-all.sql

解决方案

What I've observed that is -

if I enabled below

spring.batch.initialize-schema=always

then all the tables creates fine

 +------------------------------+
    | Tables_in_test               |
    +------------------------------+
    | batch_job_execution          |
    | batch_job_execution_context  |
    | batch_job_execution_params   |
    | batch_job_execution_seq      |
    | batch_job_instance           |
    | batch_job_seq                |
    | batch_step_execution         |
    | batch_step_execution_context |
    | batch_step_execution_seq     |

  1. If I put the below line then it only creates the report.xml

    spring.batch.schema=classpath:schema-all.sql

Is there any way if we can create both ?

这篇关于通过在 Spring Batch 中调用 .sql 文件自动创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆