3 - Spring Boot With Databasepptx
3 - Spring Boot With Databasepptx
Database
A database is an organized collection of structured data, typically stored electronically on a
computer system.
This data can include anything from text and numbers to images, videos, and audio files.
Databases are essential for storing and managing large amounts of information efficiently.
Why Database ?
Organization: They provide a structured way to store and manage vast amounts of data,
making it easier to find and retrieve specific information.
Data Integrity: Databases enforce data consistency and prevent duplication, ensuring the
accuracy and reliability of your information.
Scalability: They can grow and adapt as your data needs increase. You can add more data,
users, and functionality without significant rewrites.
Concurrency: Multiple users can access and update the database simultaneously, making it
ideal for collaborative applications.
Data Analysis: Databases facilitate data analysis by allowing you to query and filter
information based on specific criteria.
How java app use to perform
db operations
JDBC (Java Database Connectivity) API: This is the core Java API for database access. You
would use JDBC classes to:
Load the specific JDBC driver for your database (e.g., mysql-connector-java for
MySQL).
Establish a connection to the database server using connection URL, username, and
password.
Create a Statement object to execute SQL queries and updates.
Process the results of the queries or handle updates.
Spring JDBC
Spring JDBC is a module within the Spring framework that simplifies interacting with
relational databases using Java.
It builds on top of the core JDBC API but offers several advantages.
Reduced Boilerplate Code: Spring JDBC eliminates the need for manual resource
management (opening/closing connections, statements, etc.). This reduces development
time and potential errors.
Exception Handling: It provides a consistent way to handle database exceptions,
making your code more robust.
Prepared Statements: Spring JDBC encourages the use of prepared statements to
prevent SQL injection vulnerabilities, a major security concern.
JdbcTemplate: This central class in Spring JDBC offers a fluent API for executing
queries, updates, and other database operations. It simplifies common tasks and improves
code readability.
Spring JDBC
Lets build app using Spring JDBC
JDBC Template
DataSource
Spring Data JPA
JPA
JPA is a specification that simplifies object-relational mapping (ORM) in Java applications.
It provides a standard way to map Java classes (entities) to database tables and perform
CRUD operations.
JPA Advantages
Reduced Boilerplate Code:
Spring JDBC requires writing manual SQL queries and updates for CRUD operations.
JPA uses annotations and Spring Data JPA repositories,eliminating the need for manual
SQL.
Spring JDBC requires a deeper understanding of SQL,increasing development time.
JPA simplifies data access logic by focusing on entities and relationships.
Spring JDBC code might need changes when switching databases due to SQL dialects.
JPA works with various databases through JPA providers requiring minimal code
modifications.
Automatic Query Generation:Spring Data JPA generates queries based on method names.
Object-Relational Mapping (ORM):JPA simplifies mapping Java objects to database tables.
JPA Advantages
JPA Metamodel:Provides access to entity metadata at runtime for dynamic queries.
Work with JPA
Create same project with spring data jap
Configure db and JPA in application.properties file
Create ENTITES
Create REPOSITORIES
Create Service [Optional]
Use service.