Building gestion school with Spring boot & Angular

Getting Started | Building an Application with Spring Boot




Build an application enterprise with Spring boot and Angular use Jpa

Objective:

This tutoral is to develop a application of school management modules, such as the management of: Schools, activities, subjects, exams, rooms, schooling etc.... Using the different techniques and development tools mentioned.

As for the technical aspect, new technologies and good development practices have been used since Maven, which allows to create a standard tree of the code and its resources, to Framework & technologies: Hibernate / JPA, Spring, Spring security, JavaEE, Angular, Typescript... etc.


Used Skills in this project:

Java, SpringBoot, MicroServices, REST WebServices, JPA, Hibernate, MySQL Database, Maven, Angular, GitHub, SpringBoot Embedded Tomcat Server, STS-Eclipse IDE


Tools to be used

° Use any IDE to develop the Spring and Hibernate project. It may be STS/Eclipse/Netbeans. Here, we are using STS (Spring Tool Suite).

° Mysql for the database.

° Use any IDE to develop the Angular project. It may be Visual Studio Code/Sublime. Here, we are using Visual Studio Code.

° Server: Apache Tomcat/JBoss/Glassfish/Weblogic/Websphere.


Spring MVC

Spring MVC is the primary web framework built on the Servlet API. It is build on the popular MVC design pattern. MVC (Model-View-Controller) is a software architecture pattern, which separates application into three areas: model, view, and controller. The model represents a Java object carrying data. The view represents the visualization of the data that the model contains. The controller controls the data flow into model object and updates the view when the data changes. It separates the view and model.


Spring Boot Architecture



Spring Boot is a module of the Spring Framework. It is used to create stand-alone, production-grade Spring Based Applications with minimum efforts. It is developed on top of the core Spring Framework.



Introduction mysql to database

MySQL is a database management system that allows you to manage relational databases. It is open source software backed by Oracle. It means you can use MySQL without paying a dime. Also, if you want, you can change its source code to suit your needs. MySQL can run on various platforms UNIX, Linux, Windows, etc. You can install it on a server or even in a desktop. Besides, MySQL is reliable, scalable, and fast.



Introduction to the POM

POM A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/main/java; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

pom.xml

    
  
  
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0>/modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> </parent> <groupId>org.school</groupId> <artifactId>school</artifactId> <version>0.0.1-SNAPSHOT</version> <name>school</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Properties File

Properties files are used to keep ‘N’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application.properties file under the classpath.

The application.properties file is located in the src/main/resources directory. The code for sample application.properties


We need to create database name it bank

src/main/resources/application.properties

    
  
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.activemq.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.ddl-auto=update server.port=8080 spring.jpa.database=MYSQL

Class Diagram



Modals / Entities

An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.


We need to create these entities in modal package

-> Activity

-> Coefficient

-> Course

-> Exam

-> Level

-> Manager

-> Note

-> PersistableElement

-> Program

-> Room

-> School

-> Schooling

-> Subject


Here, we are creating an Entity

Activity.java This object content type, day, date, startDate, endDate and relational @ManyToOne with School

src/main/java/com/school/model/Activity.java):



package com.school.modal; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "activites") public class Activity extends PersistableElement { private static final long serialVersionUID = 1L; private String type; private String day; private String date; private String startDate; private String endDate; @JsonBackReference(value = "school") @ManyToOne private School school; public Activity() { super(); } public Activity(String type, String day, String date, School school) { super(); this.type = type; this.day = day; this.date = date; this.school = school; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDay() { return day; } public void setDate(String date) { this.date = date; } public String getDate() { return date; } public void setDay(String day) { this.day = day; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public voidsetEndDate(String endDate) { this.endDate = endDate; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } }

Here, we are creating an Entity

Coefficient.java This object content id, name, and relational @ManyToOne with Program

src/main/java/com/school/model/Coefficient.java):



package com.school.modal; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "coefficients") public class Coefficient extends PersistableElement { private static final long serialVersionUID = 1L; private int grade; @JsonBackReference(value = "program") @ManyToOne private Program program; public Coefficient() { super(); } public Coefficient(int grade, Program program) { super(); this.grade = grade; this.program = program; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public Program getProgram() { return program; } public void setProgram(Program program) { this.program = program; } }

Here, we are creating an Entity

Course.java This object content id, name, and relational @ManyToOne with Level and @OneToOne with Subject

src/main/java/com/school/model/Course.java):



package com.school.modal; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "courses") public class Course extends PersistableElement { private static final long serialVersionUID = 1L; private String date; private String startDate; private String endDate; @JsonBackReference(value = "subject") @OneToOne(cascade = CascadeType.ALL) private Subject subject; @JsonBackReference(value = "level") @ManyToOne private Level level; public Course() { super(); } public Course(String date, String startDate, String endDate, Subject subject, Level level) { super(); this.date = date; this.startDate = startDate; this.endDate = endDate; this.subject = subject; this.level = level; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; } public Level getLevel() { return level; } public void setLevel(Level level) { this.level = level; } }

Here, we are creating an Entity

Exam.java This object content id, name, and relational @ManyToOne with School and Subject and @OneToMany with Note

src/main/java/com/school/model/Exam.java):



package com.school.modal; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "exams") public class Exam extends PersistableElement { private static final long serialVersionUID = 1L; private String date; private String start; private String end; @JsonBackReference(value = "school") @ManyToOne private School school; @OneToMany(cascade = CascadeType.ALL, mappedBy = "exam") private List<Note> notes; @JsonBackReference(value = "subject") @ManyToOne private Subject subject; public Exam() { super(); } public Exam(String date, String start, String end, School school, List<Note> notes, Subject subject) { super(); this.date = date; this.start = start; this.end = end; this.school = school; this.notes = notes; this.subject = subject; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getStart() { return start; } public void setStart(String start) { this.start = start; } public String getEnd() { return end; } public void setEnd(String end) { this.end = end; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } public List<Note> getNotes() { return notes; } public void setNotes(List<Note> notes) { this.notes = notes; } public Subject getSubject() { return subject; } public void setSubject(Subject subjects) { this.subject = subjects; } public void addNote(Note note) { if (getNotes() == null) { this.notes = new ArrayList<>(); } getNotes().add(note); note.setExam(this); } }

Here, we are creating an Entity

Level.java This object content id, name, and relational @ManyToOne with Schooling and @OneToMany Course and @OneToOne with Room

src/main/java/com/school/model/Level.java):



package com.school.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "levels") public class Level extends PersistableElement { private static final long serialVersionUID = 1L; @Column(unique = true) private String label; @JsonBackReference(value = "schooling") @ManyToOne private Schooling schooling; @OneToMany(cascade = CascadeType.ALL, mappedBy = "level") private List<Course> courses; @JsonBackReference(value = "room") @OneToOne private Room room; public Level() { super(); } public Level(String label, Schooling schooling, List<Course> courses, Room room) { super(); this.label = label; this.schooling = schooling; this.courses = courses; this.room = room; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public Schooling getSchooling() { return schooling; } public void setSchooling(Schooling schooling) { this.schooling = schooling; } public List<Course> getCourses() { return courses; } public void setCourses(List<Course> courses) { this.courses = courses; } public Room getRoom() { return room; } public void setRoom(Room room) { this.room = room; } public void addCourse(Course course) { if (getCourses()== null) { this.courses = new ArrayList<>(); } getCourses().add(course); course.setLevel(this); } }

Here, we are creating an Entity

Manager.java This object content id, name, and relational @ManyToOne with School

src/main/java/com/school/model/Manager.java):



package com.school.modal; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "managers") public class Manager extends PersistableElement { private static final long serialVersionUID = 1L; private String firstName; private String lastName; @JsonBackReference(value = "school") @ManyToOne private School school; public Manager() { super(); } public Manager(String firstName, String lastName, School school) { super(); this.firstName = firstName; this.lastName = lastName; this.school = school; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName } public void setLastName(String lastName) { this.lastName = lastName; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } }

Here, we are creating an Entity

Note.java This object content id, name, and relational @ManyToOne with Exam

src/main/java/com/school/model/Note.java):



package com.school.modal; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "notes") public class Note extends PersistableElement { private static final long serialVersionUID = 1L; private double score; @JsonBackReference(value = "exam") @ManyToOne private Exam exam; public Note() { super(); } public Note(double score, Exam exam) { super(); this.score = score; this.exam = exam; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public Exam getExam() { return exam; } public void setExam(Exam exam) { this.exam = exam; } }

Here, we are creating an Entity

PersistableElement.java This object content id, createdAt, and description This class is abstract for extends for all entities

src/main/java/com/school/model/PersistableElement.java):



package com.school.modal; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.Temporal; import javax.persistence.TemporalType; @MappedSuperclass public abstract class PersistableElement implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Temporal(TemporalType.TIMESTAMP) private Date createdAt; @Column(columnDefinition = "TEXT") private String description; public long getId() { return id; } public void setId(long id) { this.id = id; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }

Here, we are creating an Entity

Program.java This object content name, and relational @ManyToOne with Subject and @OneToMany With Coefficient

src/main/java/com/school/model/Program.java):



package com.school.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "programs") public class Program extends PersistableElement { private static final long serialVersionUID = 1L; private String name; @JsonBackReference(value = "subject") @ManyToOne private Subject subject; @OneToMany(cascade = CascadeType.ALL, mappedBy = "program") private List<Coefficient> coefficients; public Program() { super(); } public Program(String name, Subject subject, List<Coefficient> coefficients) { super(); this.name = name; this.subject = subject; this.coefficients = coefficients; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; } public List<Coefficient> getCoefficients() { return coefficients; } public void setCoefficients(List<Coefficient> coefficients) { this.coefficients = coefficients; } public void addCoefficient(Coefficient coefficient) { if (getCoefficients() == null) { this.coefficients = new ArrayList<>(); } getCoefficients().add(null); coefficient.setProgram(this); } }

Here, we are creating an Entity

Room.java This object content code, capacity, and relational @ManyToOne with School and @OneToOne with Level

src/main/java/com/school/model/Room.java):



package com.school.modal; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "rooms") public class Room extends PersistableElement { private static final long serialVersionUID = 1L; private String code; private int capacity; @JsonBackReference(value = "school") @ManyToOne private School school; @JsonBackReference(value = "level") @OneToOne(cascade = CascadeType.ALL) private Level level; public Room() { super(); } public Room(String code, int capacity, School school, Level level) { super(); this.code = code; this.capacity = capacity; this.school = school; this.level = level; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } public Level getLevel() { return level; } public void setLevel(Level level) { this.level = level; } }

Here, we are creating an Entity

School.java This object content id, name, city, department, open, close and relational @OneToMany with Activity, Schooling, Exam and Room @OneToOne with Manager

src/main/java/com/school/model/School.java):



package com.school.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "schools") public class School extends PersistableElement { private static final long serialVersionUID = 1L; private String name; private String city; private String department; private String open; private String close; @OneToMany(cascade = CascadeType.ALL, mappedBy = "school") private List<Activity> activities; @OneToMany(cascade = CascadeType.ALL, mappedBy = "school") private List<Schooling> schoolings; @OneToMany(cascade = CascadeType.ALL, mappedBy = "school") private List<Exam> exams; @OneToMany(cascade = CascadeType.ALL, mappedBy = "school") private List<Subject> subjects; @OneToMany(cascade = CascadeType.ALL, mappedBy = "school") private List<Room> rooms; @JsonBackReference(value = "manager") @OneToOne(cascade = CascadeType.ALL) private Manager manager; public School() { super(); } public School(String name, String city, String department, String open, String close, List<Activity> activities, List<Schooling> schoolings, List<Exam> exams, List<Subject> subjects, List<Room> rooms, Manager manager) { super(); this.name = name; this.city = city; this.department = department; this.open = open; this.close = close; this.activities = activities; this.schoolings = schoolings; this.exams = exams; this.subjects = subjects; this.rooms = rooms; this.manager = manager; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getOpen() { return open; } public void setOpen(String open) { this.open = open; } public String getClose() { return close; } public void setClose(String close) { this.close = close; } public List<Activity> getActivities() { return activities; } public void setActivities(List<Activity> activities) { this.activities = activities; } public List<Schooling> getSchoolings() { return schoolings; } public void setSchoolings(List<Schooling> schoolings) { this.schoolings = schoolings; } public List<Exam> getExams() { return exams; } public void setExams(List<Exam> exams) { this.exams = exams; } public List<Subject> getSubjects() { return subjects; } public void setSubjects(List<Subject> subjects) { this.subjects = subjects; } public List<Room> getRooms() { return rooms; } public void setRooms(List<Room> rooms) { this.rooms = rooms; } public Manager getManager() { return manager; } public void setManager(Manager manager) { this.manager = manager; } public void addActivity(Activity activity) { if (getActivities() == null) { this.activities = new ArrayList<>(); } getActivities().add(activity); activity.setSchool(this); } public void addSchooling(Schooling schooling) { if (getSchoolings() == null) { this.schoolings = new ArrayList<>(); } getSchoolings().add(schooling); schooling.setSchool(this); } public void addExam(Exam exam) { if (getExams() == null) { this.exams = new ArrayList<>(); } getExams().add(exam); exam.setSchool(this); } public void addSubject(Subject matter) { if (getSubjects() == null) { this.subjects = new ArrayList<>(); } getSubjects().add(matter); matter.setSchool(this); } public void addRoom(Room room) { if (getRooms() == null) { this.rooms = new ArrayList<>(); } getRooms().add(room); room.setSchool(this); } }

Here, we are creating an Entity

Schooling.java This object content id, year, and relational @ManyToOne with School @OneToMany with Level

src/main/java/com/school/model/Schooling.java):



package com.school.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "schoolings") public class Schooling extends PersistableElement { private static final long serialVersionUID = 1L; private int year; @JsonBackReference(value = "school") @ManyToOne private School school; @OneToMany(cascade = CascadeType.ALL, mappedBy = "schooling") private List<Level> levels; public Schooling() { super(); } public Schooling(int year, School school, List<Level> levels) { super(); this.year = year; this.school = school; this.levels = levels; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } public List<Level> getLevels() { return levels; } public void setLevels(List<Level> levels) { this.levels = levels; } public void addLevel(Level level) { if(getLevels()==null) { this.levels = new ArrayList<>(); } getLevels().add(level); level.setSchooling(this); } }

Here, we are creating an Entity

Subject.java This object content id, name, color and relational @ManyToOne with School

src/main/java/com/school/model/Subject.java):



package com.school.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Table(name = "subjects") public class Subject extends PersistableElement { private static final long serialVersionUID = 1L; private String name; private String color; @JsonBackReference(value = "school") @ManyToOne private School school; @OneToMany(cascade = CascadeType.ALL, mappedBy = "subject") private List<Program> programs; @JsonBackReference(value = "exam") @OneToOne private Exam exam; @JsonBackReference(value = "course") @OneToOne private Course course; public Subject() { super(); } public Subject(String name, String color, School school, List<Program> programs, Exam exam, Course course) { super(); this.name = name; this.color = color; this.school = school; this.programs = programs; this.exam = exam; this.course = course; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } public List<Program> getPrograms() { return programs; } public void setPrograms(List<Program> programs) { this.programs = programs; } public Exam getExam() { return exam; } public void setExam(Exam exam) { this.exam = exam; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public void addProgram(Program program) { if(getPrograms() == null) { this.programs = new ArrayList<>(); } getPrograms().add(program); program.setSubject(this); } }


Post a Comment

Previous Post Next Post


Follow us on facebook