Giter VIP home page Giter VIP logo

sample-spring-boot-data-jpa-embedded's Introduction

Spring Boot, Spring Data JPA and Embedded h2

In almost all of my projects that involves external resources, I try my best to enable the application to fully run without dependencies. It's useful to provide a fully working backing This sample project shows how a spring-boot application can be setup with an embedded SQL database over JPA. The focus of this project is to show how to configure an embedded database with Spring Boot, however the source code also contains a RestController and a Spring Data Repository.

Other sample projects with embedded databases

Step by step

Maven dependencies To load an embedded database with Spring Boot, all you really need is to add its maven dependency into your pom. The rest will be taken care of. In my case I chose h2, so I added the following dependency:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

Spring Boot configuration

spring.datasource.url=jdbc:h2:mem:TEST;MVCC=true;DB_CLOSE_DELAY=-1;MODE=Oracle
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2

spring.datasource.initialize=true
#datasource.schema=
#datasource.data
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=none
  • spring.datasource.*: sets up an in-memory H2 database;
  • spring.datasource.initialize: tells spring-boot to initialize the database with scripts;
  • datasource.schema: the schema sql script to load. By default it is schema-${platform}.sql then schema.sql;
  • datasource.data: the data sql script. By default, it is data-${platform}.sql then data.sql;
  • spring.h2.console.enabled: allow us to access the memory database from a web interface;
  • spring.jpa.hibernate.ddl-auto: hibernates also tries to initialize the database. When it detects an embedded database, it sets ddl-auto to create-drop and initialize the database with entities annotated with @Table (and also looks for imports.sql). This may lead to creating the same table twice. I prefer to stick with Spring Boot magic, so I set this to none

That's it

Assuming you have a Spring Boot entry point, launch it:

@SpringBootApplication
public class Launcher {
    
    public static void main(String[] args){
        new SpringApplicationBuilder() //
        .sources(Launcher.class)//
        .run(args);
    }
}

Then, you can access h2's console at: localhost:8080/h2-console Simply type in the url jdbc:h2:mem:TEST;MVCC=true;DB_CLOSE_DELAY=-1;MODE=Oracle in JDBC URL field

It should work, however the database is empty.

Now simply add sql script in src/main/resources. schema-h2.sql:

CREATE TABLE MYTABLE
(
    ID NUMBER(19) NOT NULL,
    VAL VARCHAR2(50) NOT NULL,
);

data-h2.sql:

INSERT INTO MODEL (ID, VAL) VALUES (1, 'TEST');

If you go back to the h2-console, you should see your data. All that is left to do, is to create your JPARepsitory like you would normally do with Spring Data, spring boot we'll wire it up with a datasource pointing to the embedded database.

Get the code - do it

Clone the repository:

$ git clone https://github.com/alexturcot/sample-spring-boot-data-jpa-embedded.git

sample-spring-boot-data-jpa-embedded's People

Contributors

alexbt avatar

Watchers

Faisal Wirakusuma avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.