SpringBoot Framework
By: Karan G.M
INTRODUCTION
● Most popular framework.
● Includes everything you need to build an application.
● Beginner friendly.
● Production ready.
● Dependency injection is built in.
SPRING INITIALIZR
● Bootstrapping Springboot application.
● Allows to select:
○ Project Type: Maven/Graddle
○ Language: Java/Kotlin/Groovy
○ Spring version
● Defines Metadata
● Selects Java version & Packaging.
● Allows to add dependencies that our project needs.
FOLDER STRUCTURE
● Main
○ Java
■ Project package
● Model Package
○ Controllers/Models/Re
positories/Service/Con
fig
● DemoApplication(Main)
○ Resources
■ Static
■ Templates
■ Application properties
● Test
IMPLEMENTING REST API WITH SPRINGBOOT
● @RestController
● @GetMapping
● @PostMapping
● @PutMapping
● @DeleteMapping
● @RequestMapping(path=”url”)
PASSING PARAMS
● @PutMapping(path=”{studentId}”)
● Function name (@PathVariable(“studentId”) Long id,
@RequestParam(required = false) String name){}
MODEL
● Class representation of the table in the database.
● It defines all the attributes in the table.
● Provides data access to the controller.
CREATING THE MODEL
● Create a package inside jav->com.example.”project name”->”package name”.
● Inside package define the class.
● Add @Getter and @Setter
● @Entity - maps the class to the database.
● @Table - maps the class to a table.
● @NoArgsConstructor
● @AllArgsConstructor
SEQUENCE GENERATOR
● Used to generate values for primary key.
CONTROLLER
● Handles requests and response
● Connects frontend with backend
CREATING A CONTROLLER
● Create a class “ModelNameController” inside the model package.
● @RestController
● @RequestMapping(path=”url”)
● Create functions and map it to the correct request.
● Controller functions don’t have business logic.
● Calls function in the Service class for Business logic.
SERVICE
● Functions contain business logic.
● Functions invoked from Controller.
● The business logic accesses data from the database through the Model.
CONFIG
● @Configuration: indicates that the class has @Bean definition methods.
● @Bean Annotation is applied on a method to specify that it returns a bean to be
managed by Spring context.
● CommandLineRunner(“Repository Class”)
REPOSITORY
● Interface for working with JPA
● Extends JpaRepository
● Used in Service Class.
● Used for accessing database using JPQL.
● @Repository
● @Query
REST API
REST (Representational State Transfer )
● Sends request via uri to server using HTTP protocol.
● Server sends response back.
● Fixed set of Operations (GET, PUT, POST, DELETE).
● Data from server can be accessed as HTML, XML, JSON etc.
SPRING DATA JPA
WHY SPRING DATA JPA
Spring Data JPA aims to significantly improve the implementation of data
access layers by reducing the effort to the amount that's actually needed.
As a developer you write your repository interfaces, including custom
finder methods, and Spring will provide the implementation automatically.
PRIMARY KEY
CONSTRAINTS
● @Table( name=”table_name”,
uniqueConstraint = @UniqueConstraint(
name = “email_unique”,
columnNames = “email_address”
● @Column(name = “email_address”, nullable=false)
CUSTOM ATTRIBUTE NAME
● @AttributeOverride(name = "identifier", column = @Column(name = "VIN"))
FUNCTIONS THAT PERFORM QUERIES
NATIVE SQL QUERIES
● @Query(
value = “SELECT * FROM tbl_students s WHERE s.email = ?1,
nativeQuery = true
Student getStudentByEmail(String emailAddress);
NAMED PARAMS QUERIES
● @Query(
value = “SELECT * FROM tbl_students s WHERE s.email = “email_Id,
nativeQuery = true
Student getStudentByEmail(@Param(“email_Id) String emailAddress);
UPDATE & DELETE
● @Modifying
● @Transactional
● @Query(“query for updating”)
RELATIONSHIPS
WHY USE RELATIONSHIPS
● Avoid complex queries.
● Avoid Join Queries.
● Makes the code more readable.
● Easy to use.
ONE TO ONE
● Each course has one course material.
● @OneToOne (
cascade = CascadeType.All(),
fetch = FetchType.(Lazy/Eager)
)@JoinColumn(name = “course_id[name of column in table]”,
referencedColumnName = “id”[name of mapped attribute in class])
-> Do this in courseMaterials class.
BiDirectional
● @OneToOne(
mappedBy = “course[name of instance of this class in the other class]”
)->in Course class
ONE TO MANY
● One Teacher can teach many courses.
● @OneToMany(
..
)@JoinColumn( name = “teacher_id” , refferencesColumn = “teacherId”)
private List<Course> courses;
MANY TO ONE
● One Teacher can teach many courses.
● @ManyToOne(
..
)@JoinColumn( name = “teacher_id” , refferencesColumn = “teacherId”)
private List<Teacher> teacher;
MANY TO MANY
● Many students can access many courses.
● An intermediate table is created for defining the mapping.
● @ManyToMany(cascade = CascadeType.All())
@JoinTable(name = “student_course_mapping”,
joinColumn = @JoinColumn(name = “course_id”, refferencedColumnName =
“courseId”),
inverseJoinColumns = @JoinColumn(name = “student_id”,
refferencedColumnName = “StudentId”))
private List<Student> students; ->defined in course class.
THANK YOU