3.5 KiB
Spring 5 course : Udemy - Unit 2
[toc]
Unit 2: Building a Spring Boot Web App
Quick overview of Spring in a very productive way.
Generate html with dynamic content.
Disclaimer: how to use this but not going fully in details in this chapter.
- Spring Boot
- Spring MVC
- Spring Data JPA (Hibernate)
- H2 database
Spring Initializr
Back in time you needed to create you own project with maven. Spring Initializr does this job for you (maven or gradle).
https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-starters
Create a simple webapp with: https://start.spring.io/
- Spring Web (Spring mvc)
- Spring Data JPA (abstraction for hibernate)
- H2 Database (our in memory database)
Open the project in IntelliJ
For a maven project: From Existing source > Select the pom.xml file at the root of the project
Look at currated dependencies in the pom.xml by ctrl+click on the parent.artifactId
From the currated dependancies you can omit the version when you add them to your pom.xml because they are references in spring-boot-parent-2.x.x.RELEASE.pom
Session 3 - 28/10/2022 - ended here
Github Workflow
Starts from here Github workflow.
- starting source
- end source
1- fork the project and clone the repo to local
2- co fork start with starting source
3- use compare feature of git with relevant branch
Compare to source (not fork)
1st- >Copy the cloning url from original project
git remote add sfgRepo <protocol:originalrepo/url>
git fetch sfgRepo
Then in the GUI: git > compare with branch.
JPA Entities
Java Persistance API - api for Hibernate.
Modeling the data.
Creating two POJOs
Book
- title
- isbn
- authors (
as Set<Author>
)
Author
- firstName
- lastName
- books (
as Set<Book>
)
NOTE: JPA requires a 0 args construcor
Tips: Bring up the menu with alt+insert
for constructors, getters and setters
Now we are going to make them official JPA entities that can be persisted.
Leakage - object leaking up in an object database.
Assign JPA Id's
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
...
JPA Relationships
Many to many relationships. (there will be a dedicated section in the framework course).
@ManyToMany(mappedBy = 'authors')
@ManyToMany
@JoinTable(name= 'author_book', joinColumns = @JoinColumn(name= 'book_id'), inverseJoinColumns = @JoinColumn(name= 'author_id'))
Session 4 - 30/10/2022 : ended here
Equality in Hibernate
Starts from here Equality in hibernate
Leakage necessary - hibernates uses equals and hashcode methods.
Equals and HashCode
@Override
the methods to have a logic around the id property.
Tips: use alt+insert
equals() and hashCode() metods for the id
field
ToString
Tips: alt+insert
implement toString( )
Spring Data Repositories
Spring Data with JPA helps us implement repositories.
Prevents us to write all this "ceremonial code" that JDBC needs.
Create a new package repositories
For each domain object create an interface
.
Example: AuthorRepository.java
public interface AuthorRepository extends CrudRepository<Author, Long> { }
Session 5 - 31/10/2022 : ended here