I am having an issue running the schema.sql upon running the program.
In my pom.xml, I already have the mysql config:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
I only have one class
@SpringBootApplication
public class Application {...}
Under src/resources, I have schema.sql containing:
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
...
);
Under src/resources, I have application.yml containing:
spring.datasource.driverClassName: "com.mysql.jdbc.Driver"
spring.datasource.url: "jdbc:mysql://localhost:3306/sample?useSSL=false"
spring.datasource.username: "****"
spring.datasource.password: "****"
I have confirmed that I am able to connect to the database "sample" upon starting the application, however, it's not creating the table. Please advise.
That's because Spring Boot has check in DataSourceInitializer's initSchema()
method.
It will execute scripts only if your database is of type H2,DERBY,HSQL
However, you can override this behaviour by using following setting in application.properties
spring.datasource.initialization-mode=always
DataSourceInitializer.java EmbeddedDatabaseConnection.java
In my case (Spring Boot 2.0.0+) it worked as expected only when property setting spring.datasource.initialization-mode=always
was combined with spring.jpa.hibernate.ddl-auto=none
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With