Backend of Student application using java Spring part 2 #7
Автор: Badr Ouaddah
Загружено: 2023-05-29
Просмотров: 9
Описание:
4.1 Backend Walkthrough :
First thing is the backend :
This code of backend is written in Java and using the spring framework, so let’s go to see code in detail:
(Show Student class)
First, we create a student with encapsulation to secure our attribues and these attributes are name, email, date of birthday and age so we add a constructor to declare and allow us to initialize the data members of the student class.
A constructor defines itself as a standard method, but returns no value.
Then, we added Getter and Setter methods to bind variables and methods together in a single unit and prevent them from being accessed by other classes
And we make this student class as a persistent class using @Entity.
Second, we create a Student sequence function used to manage database fields of type sequence using @SequenceGenerator and @GeneratedValue.
So we add @transient in age attribute to denote that a field is not to be serialized and not to be persisted in the database.
and created a Student Controller to receive and respond to requests with @RestController and various methods annotations.
The student Controller class is annotated with @RestController used to create API RESTful web services using Spring MVC.
With the methods @GetMapping, @PostMapping, @PatchMapping, @PutMapping, @DeleteMapping, @RequestMapping.
In the Service layer of MVC Spring we create a StudentService Class with add @Service used to provide some business functionalities.
Let's pass to Student Repository you can see in this file we create an interface inherited from JpaRepository by using the extends keyword and add @Repository to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects and we put two methods are : findStudentByEmail and findStudentById.
We add into Student Service class the methods like get student, add-new-student, delete-student, update-student and findStudentById
So to create and connect to the database we create an application.properties file when I can configure and connect the database from this file.
Maven is a project build automation tool. maven makes the building process simpler, automates everyday developer goals like code compilation and tests running and dependency import and executable file creation.
We created a StudentConfig class with @Configuration and @Bean to access data and business of the application. We then added an Api Request Exception file with @ResponseStatus to mark a method or an exception class with a status code.
Finally we run this app in DemoApplication.java to see our back-end of application.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: