>  기사  >  Java  >  SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

WBOY
WBOY앞으로
2023-05-13 10:58:122634검색

    SpringBoot가 시작되고 SQL 스크립트 실행이 초기화됩니다.

    프로젝트 시작 시 일부 SQL 스크립트를 실행하려면 어떻게 해야 하나요? SpringBoot는 SpringBoot 프로젝트 시작 시 스크립트를 실행할 수 있는 이 함수를 제공합니다. , 아래를 살펴보겠습니다.

    먼저 소스코드를 살펴보자

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    boolean createSchema() {
    	//会从application.properties或application.yml中获取sql脚本列表
    	List<Resource> scripts = this.getScripts("spring.datasource.schema", this.properties.getSchema(), "schema");
        if (!scripts.isEmpty()) {
        	if (!this.isEnabled()) {
            	logger.debug("Initialization disabled (not running DDL scripts)");
                return false;
            }
    
            String username = this.properties.getSchemaUsername();
            String password = this.properties.getSchemaPassword();
            //运行sql脚本
            this.runScripts(scripts, username, password);
        }
        return !scripts.isEmpty();
    }
    private List<Resource> getScripts(String propertyName, List<String> resources, String fallback) {
    	if (resources != null) {
    		//如果配置文件中配置,则加载配置文件
        	return this.getResources(propertyName, resources, true);
       	} else {
       		//指定schema要使用的Platform(mysql、oracle),默认为all
        	String platform = this.properties.getPlatform();
            List<String> fallbackResources = new ArrayList();
            //如果配置文件中没配置,则会去类路径下找名称为schema或schema-platform的文件
            fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
           	fallbackResources.add("classpath*:" + fallback + ".sql");
           	return this.getResources(propertyName, fallbackResources, false);
        }
    }
    private List<Resource> getResources(String propertyName, List<String> locations, boolean validate) {
    	List<Resource> resources = new ArrayList();
    	Iterator var5 = locations.iterator();
    
        while(var5.hasNext()) {
        	String location = (String)var5.next();
            Resource[] var7 = this.doGetResources(location);
            int var8 = var7.length;
    
            for(int var9 = 0; var9 < var8; ++var9) {
            	Resource resource = var7[var9];
            	//验证文件是否存在
            	if (resource.exists()) {
                	resources.add(resource);
               	} else if (validate) {
                	throw new InvalidConfigurationPropertyValueException(propertyName, resource, "The specified resource does not exist.");
                }
            }
        }
        return resources;
    }

    소스코드를 보면, SpringBoot는 기본적으로 클래스 경로에서 스크립트 파일을 찾게 되지만 그 아래에는 지정된 이름만 들어갈 수 있다. 클래스 경로: 스키마 또는 스키마 플랫폼 스크립트 파일, 여러 스크립트 파일로 나누고 싶다면 이 방법이 적합하지 않습니다. 그런 다음 application.properties 또는 application.yml에서 스크립트 목록을 구성해야 합니다. 그러면 이렇게 할 수 있습니다. 초기화 스크립트 작업은 파일에 구성됩니다. 예, 세 가지 값으로 설정할 수 있는 초기화 모드 속성이 있습니다. 항상 초기화를 수행한다는 의미이며, 내장형은 메모리 데이터베이스만 초기화합니다(기본값). h3 등과 같이 초기화를 수행하지 않는다는 의미는 아닙니다.

    spring:
      datasource:
        username: root
        password: liuzhenyu199577
        url: jdbc:mysql://localhost:3306/jdbc
        driver-class-name: com.mysql.cj.jdbc.Driver
        initialization-mode: always

    이 두 가지 방법을 확인해 보겠습니다

    1. 기본적으로 스키마 또는 스키마 플랫폼의 스크립트 파일이 배치되어 있습니다

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    CREATE TABLE IF NOT EXISTS department (ID VARCHAR(40) NOT NULL, NAME VARCHAR(100), PRIMARY KEY (ID));

    데이터베이스에 부서 테이블이 없는지 확인한 후 프로그램을 시작합니다

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    시작한 후 다시 살펴보고 실행해 보겠습니다

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    2. 구성 파일에 여러 개의 SQL 스크립트가 지정되어 있습니다

    spring:
      datasource:
        username: root
        password: liuzhenyu199577
        url: jdbc:mysql://localhost:3306/jdbc
        driver-class-name: com.mysql.cj.jdbc.Driver
        initialization-mode: always
        schema:
          - classpath:department.sql
          - classpath:department2.sql
          - classpath:department3.sql

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    세 개의 SQL 스크립트는 삽입 문입니다

    INSERT INTO department (ID,NAME) VALUES (&#39;1&#39;,&#39;2&#39;)
    INSERT INTO department (ID,NAME) VALUES (&#39;2&#39;,&#39;3&#39;)
    INSERT INTO department (ID,NAME) VALUES (&#39;3&#39;,&#39;4&#39;)

    이제 데이터가 없습니다. 테이블, 다음 프로그램을 시작한 후

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    다시 살펴보겠습니다. 테이블에는 세 가지 데이터 조각이 있습니다

    SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법

    다음은 SpringBoot가 SQL 스크립트를 시작하고 초기화하는 단계입니다

    SpringBoot 프로젝트는 시작 시 지정된 sql 파일을 실행합니다

    1. 시작 시 실행

    프로젝트 시작 시 지정된 sql 문을 먼저 실행해야 할 경우 실행해야 할 sql 파일을 아래에 추가하면 됩니다. resources 폴더에 있는 sql 문은 DDL 스크립트 또는 DML 스크립트일 수 있으며 다음과 같이 해당 구성을 구성에 추가하면 됩니다.

    spring:
      datasource:
        schema: classpath:schema.sql # schema.sql中一般存放的是DDL脚本,即通常为创建或更新库表的脚本 data: classpath:data.sql # data.sql中一般是DML脚本,即通常为数据插入脚本

    2. 여러 SQL 파일 실행

    spring.datasource.schema spring.datasource.data는 둘 다 목록 수신을 지원하므로 여러 SQL 파일을 실행해야 하는 경우 다음 구성을 사용할 수 있습니다.

    spring:
      datasource:
        schema: classpath:schema_1.sql, classpath:schema_2.sql data: classpath:data_1.sql, classpath:data_2.sql 或 spring: datasource: schema: - classpath:schema_1.sql - classpath:schema_2.sql data: - classpath:data_1.sql - classpath:data_2.sql

    3. 서로 다른 실행 환경은 서로 다른 스크립트를 실행합니다.

    일반적으로 여러 실행 환경이 있습니다. 개발, 테스트, 생산 등 일반적으로 다른 운영 환경에서 실행해야 하는 SQL은 다릅니다. 이 문제를 해결하기 위해 와일드카드를 사용할 수 있습니다.

    환경별 폴더 생성

    리소스 폴더에 dev/, sit/, prod/ 등 다양한 환경에 맞는 폴더를 생성하세요.

    Configuration

    application.yml

    spring:
      datasource:
        schema: classpath:${spring.profiles.active:dev}/schema.sql 
        data: classpath:${spring.profiles.active:dev}/data.sql

    참고: ${} 와일드카드는 기본값을 지원합니다. 예를 들어 위 구성에서 ${spring.profiles.active:dev}는 세미콜론 앞의 spring.profiles.active 속성 값을 가져오고, 해당 속성 값이 존재하지 않는 경우 다음 값을 사용합니다. 세미콜론이 사용됩니다. 즉, dev입니다.

    bootstrap.yml

    spring:
      profiles:
        active: dev # dev/sit/prod等。分别对应开发、测试、生产等不同运行环境。

    알림: spring.profiles.active 속성은 일반적으로 bootstrap.yml 또는 bootstrap.properties에서 구성됩니다.

    4. 서로 다른 데이터베이스 지원

    서로 다른 데이터베이스의 구문이 다르기 때문에 동일한 기능을 달성하기 위해 서로 다른 데이터베이스의 SQL 문이 다를 수 있으므로 서로 다른 SQL 파일이 여러 개 있을 수 있습니다. 다른 데이터베이스를 지원해야 하는 경우 다음 구성을 사용할 수 있습니다.

    spring:
      datasource:
        schema: classpath:${spring.profiles.active:dev}/schema-${spring.datasource.platform}.sql
        data: classpath:${spring.profiles.active:dev}/data-${spring.datasource.platform}.sql
        platform: mysql

    알림: 플랫폼 속성의 기본값은 'all'이므로 다른 데이터베이스 간에 전환할 때 위 구성만 사용하세요. spring Boot는 현재 사용되는 데이터베이스를 자동으로 감지합니다.

    참고: 이때 dev 허용 환경을 예로 들면 resources/dev/ 폴더에schema-mysql.sql 및 data-mysql.sql 파일이 있어야 합니다.

    5. 함정 피하기

    5.1 함정

    실행된 SQL 파일에 저장 프로시저나 함수가 있는 경우 프로젝트 시작 시 오류가 보고됩니다.

    예를 들어, 이제 프로젝트가 시작되면 특정 테이블을 스캔하고, 테이블의 레코드 수가 0보다 크면 여러 레코드를 삽입하고, 건너뛰는 등의 요구 사항이 있습니다.

    schema.sql 파일 스크립트는 다음과 같습니다.

    -- 当存储过程`p1`存在时,删除。
    drop procedure if exists p1;
     
    -- 创建存储过程`p1`
    create procedure p1() 
    begin declare row_num int; select count(*) into row_num from `t_user`; if row_num = 0 then INSERT INTO `t_user`(`username`, `password`) VALUES (&#39;zhangsan&#39;, &#39;123456&#39;); end if; end; -- 调用存储过程`p1` call p1(); drop procedure if exists p1;

    프로젝트를 시작하고 오류를 보고합니다. 이유는 다음과 같습니다.

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create procedure p1() begin declare row_num int' at line 1

    大致的意思是:'create procedure p1() begin declare row_num int'这一句出现语法错误。刚看到这一句,我一开始是懵逼的,吓得我赶紧去比对mysql存储过程的写法,对了好久都发现没错,最后看到一篇讲解spring boot配置启动时执行sql脚本的文章,发现其中多了一项配置:spring.datasource.separator=$$。然后看源码发现,spring boot在解析sql脚本时,默认是以';'作为断句的分隔符的。看到这里,不难看出报错的原因,即:spring boot把'create procedure p1() begin declare row_num int'当成是一条普通的sql语句。而我们需要的是创建一个存储过程。

    5.2 解决方案

    修改sql脚本的断句分隔符。如:spring.datasource.separator=$$。然后把脚本改成:

    -- 当存储过程`p1`存在时,删除。
    drop procedure if exists p1;$$
     
    -- 创建存储过程`p1`
    create procedure p1() 
    begin declare row_num int; select count(*) into row_num from `t_user`; if row_num = 0 then INSERT INTO `t_user`(`username`, `password`) VALUES (&#39;zhangsan&#39;, &#39;123456&#39;); end if; end;$$ -- 调用存储过程`p1` call p1();$$ drop procedure if exists p1;$$

    5.3 不足

    因为sql脚本的断句分隔符从';'变成'$$',所以可能需要在DDL、DML语句的';'后加'$$',不然可能会出现将整个脚本当成一条sql语句来执行的情况。比如:

    -- DDL
    CREATE TABLE `table_name` (
      -- 字段定义
      ... 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;$$
     
    -- DML
    INSERT INTO `table_name` VALUE(...);$$

    위 내용은 SpringBoot가 SQL 스크립트 실행을 시작하고 초기화하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

    성명:
    이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제