forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-8008 - H2 running script from file (eugenp#16765)
* BAEL-8008 - H2 running script from file * BAEL-8008 - changing script file locations * BAEL-8008 - moving to another module * BAEL-8008 - moving to another module
- Loading branch information
Showing
10 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Relevant Articles | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.baeldung.boot.persistence</groupId> | ||
<artifactId>spring-boot-persistence-5</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-boot-persistence-5</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-boot-3</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../../parent-boot-3</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
44 changes: 44 additions & 0 deletions
44
...stence-modules/spring-boot-persistence-5/src/main/java/com/baeldung/h2/H2Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.baeldung.h2; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import org.h2.tools.RunScript; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@SpringBootApplication | ||
public class H2Application { | ||
public static final Logger log = LoggerFactory.getLogger(H2Application.class); | ||
|
||
@Value("${spring.datasource.url}") | ||
private String url; | ||
@Value("${spring.datasource.username}") | ||
private String user; | ||
@Value("${spring.datasource.password}") | ||
private String password; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(H2Application.class, args); | ||
} | ||
|
||
|
||
@PostConstruct | ||
public void init() throws SQLException, IOException { | ||
Connection connection = DriverManager.getConnection(url, user, password); | ||
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile())); | ||
log.info("Reading Data from the employee table"); | ||
while (rs.next()) { | ||
log.info("ID: {}, Name: {}", rs.getInt("id"), rs.getString("name")); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
persistence-modules/spring-boot-persistence-5/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
spring.datasource.url=jdbc:h2:mem:testdb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password=password | ||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect | ||
spring.jpa.hibernate.ddl-auto=none | ||
spring.sql.init.data-locations=classpath:db/data.sql | ||
spring.sql.init.schema-locations=classpath:db/schema.sql |
3 changes: 3 additions & 0 deletions
3
persistence-modules/spring-boot-persistence-5/src/main/resources/db/data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
INSERT INTO employee (name) VALUES ('John'); | ||
INSERT INTO employee (name) VALUES ('Jane'); | ||
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2; |
4 changes: 4 additions & 0 deletions
4
persistence-modules/spring-boot-persistence-5/src/main/resources/db/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE IF NOT EXISTS employee ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); |
9 changes: 9 additions & 0 deletions
9
persistence-modules/spring-boot-persistence-5/src/main/resources/db/script.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE IF NOT EXISTS employee ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
INSERT INTO employee (name) VALUES ('John'); | ||
INSERT INTO employee (name) VALUES ('Jane'); | ||
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2; | ||
UPDATE employee SET NAME = 'John Doe' WHERE ID = 1; | ||
SELECT * FROM employee; |
26 changes: 26 additions & 0 deletions
26
...spring-boot-persistence-5/src/test/java/com/baeldung/h2/H2ApplicationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baeldung.h2; | ||
|
||
import org.h2.tools.RunScript; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.FileReader; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
|
||
@SpringBootTest | ||
class H2ApplicationIntegrationTest { | ||
|
||
|
||
@Test | ||
void givenApplication_whenBootstrapped_thenDataAvailable() throws Exception { | ||
Connection connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "password"); | ||
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile())); | ||
Assertions.assertTrue(rs.next()); | ||
Assertions.assertEquals(1, rs.getInt("id")); | ||
Assertions.assertEquals("John Doe", rs.getString("name")); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
persistence-modules/spring-boot-persistence-5/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
spring.datasource.url=jdbc:h2:mem:testdb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password=password | ||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect | ||
spring.jpa.hibernate.ddl-auto=none | ||
spring.sql.init.data-locations=classpath:db/data.sql | ||
spring.sql.init.schema-locations=classpath:db/schema.sql |