springboot项目初始化执行sql

Sprint Boot应用可以在启动的时候自动执行项目根路径下的SQL脚本文件。我们需要先将sql脚本写好,并将这些静态资源都放置在src/main/resources文件夹下。

再配置application.yml:

spring.datasource.initialization-mode

      必须配置初始化模式initialization-mode,否则不生效。initialization-mode属性有always、embedded和never。

  • always表示Spring Boot应用启动时始终执行数据库初始化
  • embedded表示只初始化内存数据库,比如H2数据库
  • never表示从不执行初始化数据库

        需要注意的是,配置之后,每次启动都会执行一遍sql文件。但是我们一般都是要求只执行一次。所以我们需要在sql语句中,处理好如果已存在的处理方式。如建表语句中加入:if not exist 判断建表。

spring.datasource.platform

     spring.datasource.platform是数据库平台内容配置项,主要有mysql、postgresql、oracle等。

spring.datasource.schema

    spring.datasource.schema一般配置的是存放的是DDL脚本,即通常为创建或更新库表的脚本。该配置项时数组模式,所以可以配置多个:

application.xml:配置方式:

spring.datasource.schema[0]=classpath:sql/schema-${spring.datasource.platform:mysql}.sql
spring.datasource.schema[1]=classpath:sql/schema-${spring.datasource.platform:mysql}-1.sql

application.yml:配置方式:

spring:
  datasource:
    schema: classpath:sql/schema-${spring.datasource.platform:mysql}.sql, classpath:sql/schema-${spring.datasource.platform:mysql}-1.sql

spring:
  datasource:
    schema:
      - classpath:sql/schema-${spring.datasource.platform:mysql}.sql
      - classpath:sql/schema-${spring.datasource.platform:mysql}-1.sql

spring.datasource.data

spring.datasource.data中一般是DML脚本,即通常为数据插入脚本

该配置项时数组模式,所以可以配置多个:

application.xml:配置方式:

spring.datasource.data[0]=classpath:sql/data-${spring.datasource.platform:mysql}.sql
spring.datasource.data[1]=classpath:sql/data-${spring.datasource.platform:mysql}-1.sql

application.yml:配置方式:

spring:
  datasource:
    data: classpath:data_1.sql, classpath:data_2.sql

spring:
  datasource:
    data:
      - classpath:data_1.sql
      - classpath:data_2.sql

spring.datasource.separator

       spring.datasource.separator是配置sql的断句分割符的,默认是以';'作为断句的分隔符的。但是很多时候我们的sql语句中包含";"但是不是一整个sql语句,这时候使用”;“作为分隔符就会报错。

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 'xxxxxx' at line 1

例如我们有存储过程

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

这时候就会报错。通过 spring.datasource.separator我们就可以将默认的断句分割符改为指定值。如:spring.datasource.separator=$$。

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

 但是,由于pring.datasource.separator是全局的配置,一但将sql脚本的断句分隔符从';'变成'$$',所以需要在DDLDML语句的';'后加'$$',不然可能会出现将整个脚本当成一条sql语句来执行的情况。

猜你喜欢

转载自blog.csdn.net/qq_34484062/article/details/129303219