Create README.md
1 file changed
tree: 18d639fdd16936b59f12a946419d19eb11df9d5c
  1. .mvn/
  2. src/
  3. .gitignore
  4. mvnw
  5. mvnw.cmd
  6. pom.xml
  7. README.md
README.md

Spring-Boot-App-using-JDBC

Sample Application using H2 embedded database in spring boot

#Prerequisites

You should have the following installed 
 - Maven 
 - Tomcat

#Getting started

Create a sample Maven project with the following class DemoApplication

@SpringBootApplication
public class DemoApplication {

	// Start of a spring boot app
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

Add the following dependencies to your POM file

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
   
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
   
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
    
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>

Make a sample sql statements file and give the following in the main application to run the script

// Gets the sql statements from init.sql
	@Value("classpath:init.sql")
	private Resource initSqlScript;

	// Sql statements are executed
	@Bean
	public CommandLineRunner init(DataSource ds) {
		return args -> {
			ScriptUtils.executeSqlScript(ds.getConnection(), initSqlScript);
		};
	}

Add the model, controllers and Repositories to the code and do a maven build in eclipse or in the command prompt as follows:

mvn clean install

Run the application as spring-boot app using the following command

mvn spring-boot:run