0% found this document useful (0 votes)
4 views38 pages

SpringBoot 4KitSolutions.com Session5

The document provides an overview of Spring Boot's database connectivity, focusing on JPA and Spring Data JPA for creating CRUD applications. It explains the architecture of JPA, the advantages of using JPA over JDBC, and how to implement repositories for data access. Additionally, it details the creation of a Spring Boot REST API CRUD application using H2 database and various components like controllers and services.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views38 pages

SpringBoot 4KitSolutions.com Session5

The document provides an overview of Spring Boot's database connectivity, focusing on JPA and Spring Data JPA for creating CRUD applications. It explains the architecture of JPA, the advantages of using JPA over JDBC, and how to implement repositories for data access. Additionally, it details the creation of a Spring Boot REST API CRUD application using H2 database and various components like controllers and services.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

SPRING BOOT – PART5

[email protected]
INDEX
SB DB CONNECTIVITY

JPA

SPRING DATA JPA

CRUD REPOSITORY

JPA REPOSITORY

REST API CRUD APPLICATION


www.4kitsolutions.com
CHAPTER 1

DATABASE
CONNECTIVITY
SPRING BOOT DATABASE
 Spring Boot provides good support to create a DataSource for
Database
 We only need dependencies & configuration to create a
DataSource and connect to any Database like MSSQL, Oracle etc
 We can either use jpa or jdbc starter dependency as shown below

www.4kitsolutions.com
H2 DATABASE CONNECTION

Add h2 database dependency

Add properties related h2 db in


application.properties

www.4kitsolutions.com
OTHER DATABASES

MySQL Database dependency

Redis Database dependency

Oracle Database dependency

www.4kitsolutions.com
CHAPTER 2

JPA
JAVA PERSISTENCE API
 Java Persistence API (JPA) is open source Java standard for
mapping Java objects to a relational database

 Mapping Java objects to database tables and vice versa is


called Object-relational mapping (ORM)

 JPA is one possible approach to ORM & via JPA one can map,
store, update and retrieve data from relational databases to Java
objects and vice versa

 It uses a platform-independent object-oriented query language


JPQL (Java Persistent Query Language)
www.4kitsolutions.com
WHY JPA
 Before JPA, JDBC API was used to query relational databases

 JPA is simpler and easier to implement as compared to JDBC,


as JDBC uses native SQL queries

 JPA is suitable for non-performance oriented complex applications

 Main advantage of JPA over JDBC is that, in JPA data is represented


by objects & classes, while in JDBC data is represented by tables &
records

 It uses POJO to represent persistent data

 It supports for cross-store persistence


www.4kitsolutions.com
JPA ARCHITECTURE

www.4kitsolutions.com
JPA ARCHITECTURE
 Entity
Persistence objects stored as a records in db

 EntityManager
An interface used to control persistence operations on objects

 EntityManagerFactory
Factory class of EntityManager which creates & manages multiple
instances of EntityManager

www.4kitsolutions.com
JPA ARCHITECTURE ....
 EntityTransaction
1-to-1 relationship with EntityManager, where operations are
maintained by EntityTransaction class for each EntityManager

 Persistence
Class that contains static methods to obtain an EntityManagerFactory
instance

 Query
An interface that is implemented by each JPA vendor to obtain relation
objects that meet the criteria

www.4kitsolutions.com
JPA IMPLEMENTATION
 JPA is open source specification, and there are various vendors such
Oracle, RedHat etc providing products by adding JPA

 Some popular JPA


implementations frameworks are
Hibernate, EclipseLink,
DataNucleus etc.

 It is also known as Object-


Relation Mapping (ORM) tools
Java
Objects
 ORM layer exists between &
application & database Classes

www.4kitsolutions.com
CHAPTER 3

SPRING DATA
JPA
SPRING DATA JPA
 Spring Data JPA is not a JPA provider, rather a library or framework
that adds an extra layer of abstraction on top of our JPA provider

 Spring Data JPA can work with Hibernate, Eclipse Link or any other
JPA provider

 Spring Data JPA uses a default JPA Implementation called Hibernate

 Spring Data JPA brings in a concept of JPA Repositories a set


of Interfaces

 With Spring Data JPA, we don’t have to write a Data Access Layer or
any SQL statement at all

www.4kitsolutions.com
SPRING DATA JPA
 If we decide to use Spring Data JPA, the repository layer of our
application will contains following three layers :-
 Spring Data JPA provides support for creating JPA repositories
by extending the Spring Data repository interfaces
 Spring Data Commons provides the infrastructure that is shared
by the datastore specific Spring Data projects
 The JPA Provider implements the Java Persistence API

repositories

Hibernate (default)
www.4kitsolutions.com
SPRING DATA JPA
 The power of Spring Data JPA lies in the repository abstraction that
is provided by the Spring Data Commons project

 We can use Spring Data JPA without paying attention to actual


implementation of repository abstraction, but familiarization with the
Spring Data repository interfaces is important:-
 Repository <T, ID >

 CrudRepository <T, ID >

 PagingAndSortingRepository <T, ID >

 JpaRepository <T>

www.4kitsolutions.com
CRUD REPOSITORY
 Repository is a marker interface that has two purposes :-
 It captures the type of the managed entity and the type of the
entity’s id
 It helps the Spring container to discover the “concrete”
repository interfaces during classpath scanning

 CrudRepository is an interface, that provides CRUD operations


for the managed entity

www.4kitsolutions.com
CRUD REPOSITORY METHODS
 Create Course repository class by extending CrudRepository interface
for supporting CRUD operations using following methods :-
 findAll findById findAllById
 existsById
 save saveAll
 delete deleteAll deleteById

www.4kitsolutions.com
PAGINATION & SORTING
 PagingAndSortingRepository interface declares the methods that
are used to sort and paginate entities retrieved from the database
 It is extension of CrudRepository interface

www.4kitsolutions.com
JPA REPOSITORY
 JpaRepository interface is a JPA specific repository interface that
combines the methods declared by the common repository interfaces
behind a single interface

www.4kitsolutions.com
SPRING DATA INTERFACES

www.4kitsolutions.com
OVERALL ARCHITECTURE

Host
Application ORM Layer

Spring Data JPA

Spring Data
Commons

JPA Provider
(Hibernate)

www.4kitsolutions.com
CHAPTER 4

SRING BOOT
CRUD
APPLICATION
CRUD APPLICATION
 Building Spring Boot Rest API CRUD Application using Spring
Boot JPA to interest with H2 db

 In this example, we will use


 Spring Boot JPA
 H2 Database
 RestController
 Swagger
http://localhost:8088/swagger-ui.html/
 WebController
 Thymeleaf

www.4kitsolutions.com
DATABAES CONFIGURATION

 properties

 JPA dependency

 H2 db dependency

www.4kitsolutions.com
MODEL - JPA ENTITY
 JPA entity class is a POJO class annotated with @Entity which
represents objects in database

 Conceptually this is similar to


serializable classes, which are marked
as having the ability to be serialized

 Entities may extend both entity


and non-entity classes, and non-entity
classes may extend entity classes

 @Id - Identifies the unique ID of the


database entry

www.4kitsolutions.com
REPOSITORY
 CourseRepository class extending CrudRepository

www.4kitsolutions.com
SERVICE
 Marking beans with @Service annotation indicates class
holds business logic

www.4kitsolutions.com
REST CONTROLLER
 Creating CourseController class to support CRUD operations
 This can be tested through Swagger UI

www.4kitsolutions.com
WEB CONTROLLER
 Creating WebController class to support operations integrated with
UI
 This can be tested through Thymeleaf

www.4kitsolutions.com
RETREIVE DATA

www.4kitsolutions.com
RETREIVE DATA ....

www.4kitsolutions.com
CREATE UPDATE
CourseControllerV3

CourseService

www.4kitsolutions.com
CREATE UPDATE ....

www.4kitsolutions.com
DELETE

CourseControllerV3

CourseService

www.4kitsolutions.com
DELETE ....

www.4kitsolutions.com
THANK YOU
HAPPY LEARNING!!

[email protected]

You might also like