- Spring Security(Third Edition)
- Mick Knutson Robert Winch Peter Mularien
- 318字
- 2025-04-04 17:54:29
Initializing the database
We can now remove the src/main/resources/database directory and all contents in that directory. This directory contains several .sql files, and we will consolidate and move them to the next step:
Now, we need to create a data.sql file that will contain our seed data, as follows:
//src/main/resources/data.sql:
- Take a look at the following SQL statement, depicting the password for user1:
insert into calendar_users(id,username,email,password,
first_name,last_name)
values(0,'user1@example.com','user1@example.com',
'$2a$04$qr7RWyqOnWWC1nwotUW1nOe1RD5.
mKJVHK16WZy6v49pymu1WDHmi','User','1');
- Take a look at the following SQL statement, depicting the password for admin1 :
insert into calendar_users(id,username,email,password,
first_name,last_name)
values (1,'admin1@example.com','admin1@example.com',
'$2a$04$0CF/Gsquxlel3fWq5Ic/ZOGDCaXbMfXYiXsviTNMQofWRXhvJH3IK',
'Admin','1');
- Take a look at the following SQL statement, depicting the password for user2:
insert into calendar_users(id,username,email,password,first_name,
last_name)
values (2,'user2@example.com','user2@example.com',
'$2a$04$PiVhNPAxunf0Q4IMbVeNIuH4M4ecySWHihyrclxW..PLArjLbg8CC',
'User2','2');
- Take a look at the following SQL statement, depicting the user roles:
insert into role(id, name) values (0, 'ROLE_USER');
insert into role(id, name) values (1, 'ROLE_ADMIN');
- Here, user1 has one role:
insert into user_role(user_id,role_id) values (0, 0);
- Here, admin1 has two roles:
insert into user_role(user_id,role_id) values (1, 0);
insert into user_role(user_id,role_id) values (1, 1);
- Take a look at the following SQL statement, depicting events:
insert into events (id,when,summary,description,owner,attendee)
values (100,'2017-07-03 20:30:00','Birthday Party',
'This is going to be a great birthday',0,1);
insert into events (id,when,summary,description,owner,attendee)
values (101,'2017-12-23 13:00:00','Conference Call','Call with
the client',2,0);
insert into events (id,when,summary,description,owner,attendee)
values (102,'2017-09-14 11:30:00','Vacation',
'Paragliding in Greece',1,2);
Now, we can update the application properties to define our embedded database properties in the src/main/resources/application.yml file as follows:
# Embedded Database
datasource:
url: jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driverClassName: org.h2.Driver
username: sa
password:
continue-on-error: true
jpa:
database-platform: org.hibernate.dialect.H2Dialect
show-sql: true
hibernate:
ddl-auto: create-drop
At this point, we have removed the old database configuration and added the new configuration. The application will not work at this point, but this can still be considered a marker point before we continue on to the next steps of conversion.
Your code should now look like calendar05.01-calendar .