SPRING BOOT – PART4
[email protected]
INDEX
Memory Database
Embedded Database – H2
Spring Boot – Memory DB – H2
THYMELEAF
ModelAndView
www.4kitsolutions.com
CHAPTER 1
IN-MEMORY DB
WHY ANOTHER TYPE OF DB
Typical databases involves lot of setup. For example, Oracle or MS SQL
databases will require following steps:-
Provisioning of new Server
Installation of database on Server
Setting up complex configuration
Setting up Schema, table etc
Open network ports like 1521, 1433 etc for applications to connect
Connect application to DB using data source and lot of other code......
Case1 – Let’s consider a use case, where we want to perform quick POC,
but traditional database involves lot of overhead
Case2 – We wanted to develop Microservice using lightweight database
Case3 – Fast response time for static data
Case4 – Need fast, cost efficient and manageable database
www.4kitsolutions.com
MEMORY DATABASE
In-memory databases are designed to attain minimal response
time by eliminating need to access disk over network
App Memory DB
(No n/w call)
App
App
Database Access
over Network
App
www.4kitsolutions.com
MEMORY DATABASE ....
In-memory or Memory database is a type of purpose-built
database that relies primarily on memory (RAM) for data
storage, in contrast to databases that store data on disk or SSDs
In-memory database can be used to manage Caching - A cache
is a high-speed data storage layer which stores a subset of data,
typically transient in nature, so that future requests for that data
are served up faster
www.4kitsolutions.com
MEMORY DATABASE ....
Also known by different aliases
IMDB – In-Memory database
MMDB – Main memory database
RTDB – Real-time database
IMDS – In-Memory database system
Memory resident database
Downside is volatility of RAM i.e. data is lost if memory
database shuts down or crashes
RAM is costlier as compared to HDD, SSD
www.4kitsolutions.com
MEMORY DATABASE ....
Advantages of Memory Database are :-
Minimal Configuration & Maintenance
Faster and real-time
Zero project setup or infrastructure
Easy to use for Learning, POCs and Unit Tests
Spring Boot provides simple configuration to switch between real
database & in-memory database like H2
Popular Memory Databases are :-
H2 Hazelcast
Redis Apache Ignite
SQLite Aerospike
Apache Derby PostgresSQL
Amazon Elasticache for Redis Google Cloud Memorystore
www.4kitsolutions.com
H2 DATABASE
H2 is an open-source lightweight relational Java database.
H2 database can be configured to run as in-memory database,
which means data will not persist on disk
H2 db is mostly used for POC, Testing, cache implementation etc.
This database can be used in embedded mode or in server mode
Main features of H2 db are:-
https://www.h2database.com/
Extremely fast, open source, JDBC API
Available in embedded & server modes; in-memory databases
Browser-based Console application
Small footprint − Around 2MB jar file size
Supports clustering and multi-version concurrency
www.4kitsolutions.com
H2 DATABASE & SPRING BOOT
We need very little configuration to connect Spring Boot application with
H2 DB. In most situations, just adding H2 runtime jar into dependencies
should be sufficient
www.4kitsolutions.com
H2 CONSOLE
H2 provides a web interface called H2
Console to see data, which is disabled by
default. It can be enabled using following
property
H2 console can be accessed using
following URL
http://localhost:8088/h2-console
www.4kitsolutions.com
H2 DB CONFIGURATION
We can change default configuration using following properties
www.4kitsolutions.com
CREATE & SELECT TABLE
We can create table using H2 Console using SQL as shown below
CREATE TABLE COURSE (
COURSEID VARCHAR(10) PRIMARY KEY,
COURSENAME VARCHAR(50) NOT NULL
);
H2 is in-memory db, which
is volatile by design & hence
all data will be lost when we
restart our spring boot
application
www.4kitsolutions.com
H2 DB PERSISTENCE
We can change behaviour of H2 db from memory based to file
based using following property :-
www.4kitsolutions.com
DB OPERATIONS
Now after restarting application, data will be retained
www.4kitsolutions.com
DATABASE COMPARISON
Server
Network
Database Access
App over Network
Minimum Response Time
Client
Server
Memory DB - RAM
App (No n/w call)
Client Maximum Response Time
Server
App Memory DB – File system
(No n/w call)
Client
www.4kitsolutions.com
CHAPTER 2
THYMELEAF
THYMELEAF
Thymeleaf is an open-source Java library (Apache License 2.0),
which is used to create a web application
It is a HTML5/XHTML/XML template engine
The goal of Thymeleaf is to provide a stylish and well-formed way of
creating templates
Thymeleaf can process six types of templates
XML
Valid XML
XHTML
Valid XHTML
HTML5
Legacy HTML5
www.4kitsolutions.com
THYMELEAF SETUP
Add dependency, Controller with unique URI, html & styling file
www.4kitsolutions.com
THYMELEAF SETUP ....
\resources\templates\index.html
\resources\static\css\styles.css
www.4kitsolutions.com
THYMELEAF OUTPUT
In this example, request URI is /index and controller is
redirected to index.html
www.4kitsolutions.com
THYMELEAF SWAGGER
www.4kitsolutions.com
@RESTCONTROLLER VS @CONTROLLER
@RestController is combination of @Controller & @ResponseBody,
whose main purpose is to create RESTful web services
RestController returns json from method, not HTML or JSP
For returning html or jsp, simply annotate controller class with
@Controller
www.4kitsolutions.com
@RESTCONTROLLER VS @CONTROLLER
@Controller
When we use spring as SpringMVC
@RestController
When we use spring as SpringRESTfull Web Service
www.4kitsolutions.com
MODELANDVIEW
This is holder for both Model & View in web MVC framework
This class merely holds both to make it possible for a controller to
return both model & view in a single return value
View
Modal
www.4kitsolutions.com
MODELANDVIEW EXAMPLE
First step is to create
Controller with unique URI
like /input
Create input.html, which
will be returned by controller
at
\resources\templates\index.html
www.4kitsolutions.com
MODELANDVIEW EXAMPLE ....
Create final html view course-
data.html
Create ModelAndView which
will hold both Model i.e. Course and
our newly created View i.e. course-
data.html
www.4kitsolutions.com
MODELANDVIEW EXAMPLE OUTPUT
input.html
course-data.html
www.4kitsolutions.com
THANK YOU
HAPPY LEARNING!!
[email protected]