Introduction: There are two datasources in my spring boot application and default database is mysql. I want to create a schema and insert data in h2 database.
Actual Result: Create them into mysql database.
My Try: I study with spring-boot-loading-initial-data. and set spring.sql.init.platform=h2 and change sql name to data/schema-h2.sql. based on document 18.9.3. Initialize a Database Using Basic SQL Scripts
Expect Result: Create schema and insert data into h2 database when spring boot application launches.
Here are my Code:
#application.properties
spring.datasource.sql.jdbc-url=jdbc:mysql://localhost:3306/test?useSSL=false&autoreconnect=true&zeroDateTimeBehavior=convertToNull
spring.datasource.sql.username=root
spring.datasource.sql.password=
spring.datasource.sql.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.h2.jdbc-url=jdbc:h2:mem:mydb
spring.datasource.h2.driverClassName=org.h2.Driver
spring.datasource.h2.username=sa
spring.datasource.h2.password=password
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
spring.sql.init.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.sql.init.mode=always
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
#schema-h2.sql
CREATE TABLE country (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
PRIMARY KEY (id)
);
#data-h2.sql
INSERT INTO country (name) VALUES ('India');
INSERT INTO country (name) VALUES ('Brazil');
INSERT INTO country (name) VALUES ('USA');
INSERT INTO country (name) VALUES ('Italy');
#DBConfig.java
package com.example.h21;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DBConfig {
@Bean(name = "h2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.h2")
public DataSource h2DataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "sqlDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sql")
public DataSource sqlDataSource() {
return DataSourceBuilder.create().build();
}
@Autowired
@Bean(name = "h2JdbcTemplate")
public JdbcTemplate h2JdbcTemplate(@Qualifier("h2DataSource") DataSource h2DataSource) {
return new JdbcTemplate(h2DataSource);
}
@Primary
@Bean(name ="sqlJdbcTemplate")
@Autowired
public JdbcTemplate sqlJdbcTemplate(@Qualifier("sqlDataSource") DataSource sqlDataSource) {
return new JdbcTemplate(sqlDataSource);
}
}