项目作者: aparnaittekot

项目描述 :
Sample Application using H2 embedded database in spring boot
高级语言: Java
项目地址: git://github.com/aparnaittekot/Spring-Boot-App-using-JDBC.git


Spring-Boot-App-using-JDBC

Sample Application using H2 embedded database in spring boot

Prerequisites

  1. You should have the following installed
  2. - Maven
  3. - Tomcat

Getting started

Create a sample Maven project with the following class DemoApplication

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. // Start of a spring boot app
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }

The H2 database is taken as default (configuration file is not required in this case), so just we need to give its dependencies in the pom file

Add the following dependencies to your POM file

  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <scope>test</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-jdbc</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.h2database</groupId>
  20. <artifactId>h2</artifactId>
  21. </dependency>

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

  1. // Gets the sql statements from init.sql
  2. @Value("classpath:init.sql")
  3. private Resource initSqlScript;
  4. // Sql statements are executed
  5. @Bean
  6. public CommandLineRunner init(DataSource ds) {
  7. return args -> {
  8. ScriptUtils.executeSqlScript(ds.getConnection(), initSqlScript);
  9. };
  10. }

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

  1. mvn clean install

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

  1. mvn spring-boot:run