How to handle Database in Spring Boot
Tags: security, database, serverless, spring
Published: 2020-05-08 01:23:29


Core data for Spring Boot with Database. Please you may use more source in there [link] (https://github.com/Urunov/SpringBoot-Database). 

This provides Database implementation in the Spring Boot. Especially, we'll  briefly inform here concept of Spring, Spring Boot, JDBC, JPA, H2 and result of our experience project(there is link using github).

# Spring

The Spring Framework is an application framework and inversion of control container for the Java platform. Spring is HA (high avilability).
Spring Boot

Most Spring Boot applications need minimal Spring configuration. Features. Create stand-alone Spring applications

# JPA

JPA (The Java Presistence API) = This module deals with enhanced support for JPA based data access layers.Indeed, the JPA is set of the rules to interfaces. 
  * JPA follows Object-Relation Mapping (ORM). It is a set of interfaces. It also provides a runtime EntityManager API for processing queries and transactions on the objects against the database. It uses a platform-independent object-oriented query language JPQL (Java Persistent Query Language).
Why should we use JPA? JPA is simpler, cleaner, and less labor-intensive than JDBC, SQL, and hand-written mapping. JPA is suitable for non-performance oriented complex applications.

API (Application programming interface) is a document that contains a description of all the features of a product or software. It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems, etc.

JPA Implementations JPA is an open-source API. There is various enterprises vendor such as Eclipse, RedHat, Oracle, etc. that provides new products by adding the JPA in them. There are some popular JPA implementations frameworks such as Hibernate, EclipseLink, DataNucleus, etc. It is also known as Object-Relation Mapping (ORM) tool.

#JDBC

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database.
Architecture of the Project Implementation.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/glx02kwoyoeq3k9ck73r.JPG)

# H2

  * H2 is a relational database management system written in Java. It can be embedded in Java applications or run in client-server mode.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/noa3b0p9djq4o7zmy9a6.JPG)

# Spring Boot + JPA + Hibernate + MySQL|Oracle

Real project configuration. [Source](https://github.com/Urunov/SpringBoot-Database).
 
Create a Maven Project
You may use your tool(Eclipse, IntelliJ Idea, etc.), this example we used IntelliJ then create a new Maven project and name it SpringJDBC.

At the end of this tutorial, we’ll get the following project structure:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/267vzg8rks9t9t10i1wg.JPG)

## POM.XML
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
     </parent>

* Configuration DB
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fm5xz2ec634dc116mufh.jpg)

* Repositories
We define our repositories' interfaces under spring.jdbc.dao. Each repository extends Spring CrudRepository, which provides a default implementation for the basic find, save, and delete methods — so we don’t care about defining implementation classes for them.