H2 database is a inmemory data base which is embedded and we can use it as in memory database. it is quiet easy to use H2 database if you are working with small scale application when you just need to dumb data in db and use it later in the computation.
Advantage of using H2
- It will not used application memory and will use the separate memory on same host.
- It is less cheaper to use than database which are not in memory.( network databases are expensive in term of speed ) when there are not much data need to store.
Now let see the implementation of H2 Database in spring boot
You can download the project from GitHub
Lets first see the project structure
Now if we see the pom.xml what all the dependency we have added in the project
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.programinjava.learn</groupId> <artifactId>H2DbInSpringBootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>H2DbInSpringBootDemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- Spring- Web
- JPA
- H2 database
Now let see the application.properties what we have added in it to configure the H2 Database
# H2 spring.h2.console.enabled=true spring.h2.console.path=/h2 # Datasource spring.datasource.url=jdbc:h2:file:~/test spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver
Now there are new things we need to understand here
spring.h2.console.path=/h2Here we can provide what path we want to give to see the H2 Console ex- /h2-console , /h2
spring.h2.console.enabled=trueif this value will be true then only we will be able to see the H2 Console in browser.
Now let see how to access the H2 Console
- Once you run the spring boot project , you can just go to the below url to see the h2 console.
After clicking on Connect button , you just see the below DB Console. when you can do all the operations.
That's all you need to do , Congratulations you are now using H2 database in your spring boot application.
Also Read:
- Spring Boot and Hibernate Tutorials - Application using Spring Boot
- What is Spring boot ?
- How to read value from property file in spring boot ?
- First Rest API Application - Top Spring Framework Interview Questions
- Implementation of Swagger in Spring boot
Hope this will help you in using H2 in spring boot.
Please share it with your friends and colleagues .
Thanks for reading
0 comments:
Post a Comment