通过在Spring Batch中调用.sql文件自动创建表?

时间:2018-07-05 20:19:12

标签: spring spring-boot spring-batch

我关注了Spring Batch Framework - Auto create Batch Tablehttps://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/#howto-initialize-a-spring-batch-database之类的许多链接,但这不能满足我的要求

在Spring Boot 2.0中,我们如何调用sql文件(手动创建并具有所有模式详细信息),以便Spring Batch可以为我们手动创建它,如果模式已经存在,则跳过执行?

<!-- 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>

我想做 1.如果已经存在架构,则不要创建任何表 2.如果没有架构,则创建表

项目结构

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;

enter image description here

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

2 个答案:

答案 0 :(得分:2)

我观察到的是-

如果我在下面启用了

  

spring.batch.initialize-schema =总是

然后所有表都可以创建

targets
  1. 如果我将以下行放入,则仅创建report.xml
      

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

我们可以同时创建这两者吗?

答案 1 :(得分:0)

您可以使用org.springframework.jdbc.datasource.init.ScriptUtils类手动运行sql文件。 可能不是全部解决方案,但可以帮助您实现目标。

import java.nio.charset.Charset;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Component;

@Component
public class SqlUtil {
    @Autowired private ApplicationContext context;
    @Autowired private DataSource datasource;

    // SQLFileName could be your schema-all.sql
    // inject this class somewhere and call sqlUtil.runSqlFile("schema-all.sql");
    public void runSqlFile(String SQLFileName) {
        Resource resource = context.getResource(SQLFileName);
        EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8"));
        try {
            ScriptUtils.executeSqlScript(datasource.getConnection(), encodedResource);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}

此外,mysql可以使用IF NOT EXISTS来检查表是否存在,如the doc所示。 如果不存在,下面的代码将创建一个表:

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

要检查架构是否存在,也许您可​​以使用此other topic中所示的内容:

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'