-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH2 Database Setup..
More file actions
164 lines (126 loc) · 3.97 KB
/
Copy pathH2 Database Setup..
File metadata and controls
164 lines (126 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Setting Up The Database (H2)...
In the previous demo, you:
Added a book using POST
Fetched it using GET
Opened the database
Saw the BOOK table
You saw that your Java object was actually stored inside a real database table.
Now the important question is:
Which database were we using?
The Database We Used: H2
In our previous example, we were using a database called H2.
H2 is:
A lightweight, high-performance database management system written in Java
Very easy to set up
Runs inside your Spring Boot application
Perfect for learning and testing
That’s why we used it for this demo.
Now, let’s properly configure H2 step-by-step:
Add H2 dependency
Configure application.properties
Open H2 console
Next, Let’s start with adding the dependency.
##################################
H2 Database Setup
To use H2 database in our Spring Boot project, we must add its dependency.
Step 1: Adding H2 Dependency
If you are using Spring Initializr to download the dependency as you've learned earlier then, just select:
Spring Data JPA
H2 Database
If adding manually in pom.xml, add this:
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- H2 Database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
spring-boot-starter-data-jpa
This dependency:
Adds JPA support
Adds Hibernate (ORM implementation)
Enables repository features like:
bookRepository.save(book);
bookRepository.findAll();
You don’t write SQL manually.
JPA + Hibernate handle database operations for you.
h2 Dependency
This dependency:
Adds H2 database to our project
Allows Spring Boot to connect to it
Enables H2 console
Without this, database won’t work.
Step 2: Configure application.properties
Now we tell Spring Boot how to connect to H2.
Open:
src/main/resources/application.properties
Add this configuration:
# H2 Database Config
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Show SQL in console
spring.jpa.show-sql=true
# Automatically create tables
spring.jpa.hibernate.ddl-auto=update
# Enable H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true
spring.h2.console.settings.trace=false
What These Important Lines Mean
jdbc:h2:mem:testdb
mem means in-memory database
Data exists while app is running
Restart → data gone
ddl-auto=update
This tells Hibernate:
Automatically create or update tables based on our @Entity classes.
So when we create:
@Entity
public class Book { ... }
Table is automatically created.
No manual SQL needed.
show-sql=true
This prints SQL queries in console.
So when we call:
bookRepository.save(book);
You will see:
insert into book ...
This helps students see ORM in action.
Step 3: Open H2 Console
After running the application, open:
If port is 3000:
http://localhost:3000/h2-console
Login with:
JDBC URL: jdbc:h2:mem:testdb
Username: sa
Password: (leave empty)
Click Connect.
Now you can see your tables.
What Just Happened?
When the app starts:
Spring Boot connects to H2
Hibernate reads @Entity
Table is created automatically
Repository works with database
All automatically.
That’s the power of Spring Boot + JPA + H2.
#########################################
H2 Database - MCQ 1
Without adding the H2 dependency, what will happen?
ANSWER = There will be no database to store data
Because H2 is the database.
Without adding H2 (or any DB dependency), there is no database available to store data.
######################################
H2 Database - MCQ 2
Which ORM framework is included automatically when we add spring-boot-starter-data-jpa?
ANSWER = Hibernate
When we add spring-boot-starter-data-jpa, Spring Boot automatically includes Hibernate as the default ORM implementation.
#################################