当前位置: 首页 > news >正文

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()
begindeclare row_num int;select count(*) into row_num from `t_user` where id = 'root';if row_num = 0 thenINSERT 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()
begindeclare row_num int;select count(*) into row_num from `t_user` where username = 'root';if row_num = 0 thenINSERT 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语句来执行的情况。

http://www.lryc.cn/news/26680.html

相关文章:

  • Kubernetes之存储管理(中)
  • MySQL workbench的基本操作
  • 【Flink】FlinkSQL中Table和DataStream互转
  • 网络总结知识点(网络工程师必备)一
  • 离线安装samba与配置(.tar方式安装)
  • [Java基础]—JDBC
  • 基本面向对象编程-计算机基本功能实现_
  • C++面向对象之多态性
  • Android性能优化系列篇:弱网优化
  • Mysql 插入大批量数据调优方法
  • matlab基础
  • 自动化测试——多窗口切换和切换frame
  • C#中,Elasticsearch.Net判断空字符串
  • 23种设计模式-适配器模式
  • 深入理解this指向问题
  • 事业单位联考(综合应用A类)典型例题教案
  • frp内网穿透实验
  • 认识JavaScript中的防抖函数
  • macOS 13.3 Beta 2 (22E5230e)With OpenCore 0.8.9正式版 and winPE双引导分区原版镜像
  • JetPack—DataStore核心原理与使用
  • 热烈祝贺|酒事有鲤盛装亮相2023中国(山东)精酿啤酒产业发展创新论坛暨展览会
  • 深度强化学习DLR
  • Android Handler机制(四) Message源码分析
  • 【Git】git命令(全)
  • 软考论文-成本管理(1)
  • Java 多线程 --- 锁的概念和类型划分
  • python程序员狂飙上头——京海市大嫂单推人做个日历不过分吧?
  • 浅谈子网掩码、IP地址、网络地址之间关系
  • 前端优化的解决方案
  • PYthon组合数据类型的简单使用