The Sprint Boot application can automatically execute the SQL script file in the root path of the project when it starts. We need to write the sql script first, and place these static resources in the src/main/resources folder.
Then configure application.yml:
spring.datasource.initialization-mode
The initialization mode initialization-mode must be configured, otherwise it will not take effect. The initialization-mode attributes are always, embedded, and never.
- always indicates that the database initialization is always performed when the Spring Boot application starts
- embedded indicates that only the memory database is initialized, such as the H2 database
- never means never initialize the database
It should be noted that after configuration, the sql file will be executed every time it is started. But we generally require only one execution. So we need to deal with the existing processing method in the sql statement. For example, add in the table creation statement: if not exist to judge the table creation.
spring.datasource.platform
spring.datasource.platform is a database platform content configuration item, mainly including mysql, postgresql, oracle, etc.
spring.datasource.schema
spring.datasource.schema is generally configured to store DDL scripts, that is, scripts that usually create or update database tables. This configuration item is an array mode, so you can configure multiple:
application.xml: configuration method:
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: configuration method:
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 is generally a DML script, which is usually a data insertion script
This configuration item is an array mode, so you can configure multiple:
application.xml: configuration method:
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: configuration method:
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 configures the sentence separator of sql, and the default is ';' as the sentence separator. But many times our sql statement contains ";" but not a whole sql statement. At this time, using ";" as a delimiter will cause an error.
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
For example we have stored procedure
-- 当存储过程`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;
At this time, an error will be reported. Through spring.datasource.separator, we can change the default sentence separator to a specified value. Such as: 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;$$
However, since pring.datasource.separator is a global configuration, once sql
the sentence separator of the script ';'
is changed from '$$'
, it needs to be added after DDL
the DML
statement , otherwise the whole script may be executed as a statement.';'
'$$'
sql