BAEL-1980 - Sharing H2 database (#5039)
* Added Spring-Boot-H2 project * Optimized imports and formatted code * Code Formatting changes * Renamed the client applicaiton * Removed Name from pom.xml * formatting changes - applied formatting style * added souts * Update SpringBootApp.java * Update ClientSpringBootApp.java * Fixed Formatting issues * Fixed formatting and spelling mistakes
This commit is contained in:
parent
c1db9c7c1c
commit
93339f4828
|
@ -0,0 +1,25 @@
|
|||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.h2db</groupId>
|
||||
<artifactId>spring-boot-h2-database</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>Demo Spring Boot applications that starts H2 in memory database</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.h2db.demo;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.h2.tools.Server;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootApp {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootApp.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void initDb() {
|
||||
System.out.println(String.format(
|
||||
"****** Creating table: %s, and Inserting test data ******", "Employees"));
|
||||
|
||||
String sqlStatements[] = {
|
||||
"drop table employees if exists",
|
||||
"create table employees(id serial,first_name varchar(255),last_name varchar(255))",
|
||||
"insert into employees(first_name, last_name) values('Eugen','Paraschiv')",
|
||||
"insert into employees(first_name, last_name) values('Scott','Tiger')"
|
||||
};
|
||||
|
||||
Arrays.asList(sqlStatements).stream().forEach(sql -> {
|
||||
System.out.println(sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
});
|
||||
|
||||
System.out.println(String.format("****** Fetching from table: %s ******", "Employees"));
|
||||
jdbcTemplate.query("select id,first_name,last_name from employees",
|
||||
new RowMapper<Object>() {
|
||||
@Override
|
||||
public Object mapRow(ResultSet rs, int i) throws SQLException {
|
||||
System.out.println(String.format("id:%s,first_name:%s,last_name:%s",
|
||||
rs.getString("id"),
|
||||
rs.getString("first_name"),
|
||||
rs.getString("last_name")));
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public Server inMemoryH2DatabaseServer() throws SQLException {
|
||||
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
spring.datasource.url=jdbc:h2:mem:mydb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
|
@ -0,0 +1,25 @@
|
|||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.h2db</groupId>
|
||||
<artifactId>spring-boot-h2-remote-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>Demo Spring Boot applications that access H2 in memory database created
|
||||
in another Spring Boot application
|
||||
</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.h2db.demo;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ClientSpringBootApp {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ClientSpringBootApp.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void initDb() {
|
||||
System.out.println("****** Inserting more sample data in the table: Employees ******");
|
||||
String sqlStatements[] = {
|
||||
"insert into employees(first_name, last_name) values('Donald','Trump')",
|
||||
"insert into employees(first_name, last_name) values('Barack','Obama')"
|
||||
};
|
||||
|
||||
Arrays.asList(sqlStatements).stream().forEach(sql -> {
|
||||
System.out.println(sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
});
|
||||
|
||||
System.out.println(String.format("****** Fetching from table: %s ******", "Employees"));
|
||||
jdbcTemplate.query("select id,first_name,last_name from employees",
|
||||
new RowMapper<Object>() {
|
||||
@Override
|
||||
public Object mapRow(ResultSet rs, int i) throws SQLException {
|
||||
System.out.println(String.format("id:%s,first_name:%s,last_name:%s",
|
||||
rs.getString("id"),
|
||||
rs.getString("first_name"),
|
||||
rs.getString("last_name")));
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
spring.datasource.url=jdbc:h2:tcp://localhost:9091/mem:mydb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create
|
Loading…
Reference in New Issue