0% found this document useful (0 votes)
57 views

Steps For Creating Spring Boot Application

The document outlines the 5 main steps to create a Spring Boot application: 1. Use Spring Initializr to set up dependencies like Spring Web, Data JPA, and MySQL Driver. 2. Create the database and tables. 3. Create model, repository, service, and controller packages to define entities, CRUD operations, and request mappings. 4. Configure the project using an application.properties file to define database properties. 5. Build the front-end using Thymeleaf templates to display and manipulate data.

Uploaded by

Mariem Raz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Steps For Creating Spring Boot Application

The document outlines the 5 main steps to create a Spring Boot application: 1. Use Spring Initializr to set up dependencies like Spring Web, Data JPA, and MySQL Driver. 2. Create the database and tables. 3. Create model, repository, service, and controller packages to define entities, CRUD operations, and request mappings. 4. Configure the project using an application.properties file to define database properties. 5. Build the front-end using Thymeleaf templates to display and manipulate data.

Uploaded by

Mariem Raz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Steps of creating spring boot application

1. Spring initializr :
- Group.
– Artifact.
– Dependencies (Spring Web: RESTfull, Spring Data JPA: persistence, Thymeleaf: HTML,
MySQL Driver: database).
2. Create the database and its tables.
3. Creating packages :
- Model: It must have @Entity in the first and @Id @GeneratedValue(strategy =
GenerationType.IDENTITY) for the ID that auto increments.
 One-to-One Relationship :
. The entity with the foreign key in its table is the child.
. The parent (artist) should use @OneToOne(mappedBy = “artist”,
cascade = CascadeType.ALL, fetch = FetchType.LAZY). The key here is
mappedBy. This tells JPA that rankings are mapped by the artist entity on the
ranking object.
. The child (ranking) should have @OneToOne and
@JoinColumn(name = “artist_id”). This tells JPA that Artist is mapped to
ranking via the artist_id.
 One-to-Many Relationship:
. The entity with the foreign key in its table is the child.
. The parent (artist) should use @OneToMany(mappedBy = “artist”,
cascade = CascadeType.ALL, fetch = FetchType.LAZY). The key here is
mappedBy. This tells JPA that rankings are mapped by the artist entity on
the ranking object.
. The child (recording) should have @ManyToOne and
@JoinColumn(name = “artist_id”). This tells JPA that Artist is mapped to
ranking via the artist_id.
 Many-to-Many Relationship:
. Choose one of the entities, doesn’t matter which one, to hold the
@JoinTable, consider this the parent entity.
.The parent (recording) must define its own foreign key as the join
column joinColumns = { @JoinColumn(name = “recording_id”) },.
.The parent (recording) entity must define the child as the
inverseJoinColumns.  inverseJoinColumns = { @JoinColumn(name =
“release_id”)
.The child (release) uses @ManyToMany(mappedBy = “releases”) to
map the parent to itself.
Source : https://tenmilesquare.com/resources/software-development/spring-boot-jpa-relationship-
quick-guide/

– Repository: It must have @Repository in the first. (interface that extends


CrudRepository<entity class, class of id type> or extends JpaRepository<entity class, class of
id type>).
– Service: It must have @Service in the first. (Crud operations  save, find and delete). It
contains repository as an attribute with @Autowired Pour qu’un bean soit injecté dans un
attribute (injection de la dépendence) et éviter new ().
– Controller: It must have @Controller in the first. (Contains methods that calls the html (it
adds attribute to the model class if necessary and returns the name of the html file or
“redirect:/”) and other methods that matches every operation in the service with a mapping
and indicate if it will be called by get or post… With @GetMapping("/ejeizjio"),
@RequestMapping("/edit/{id}") or @RequestMapping(value = "/save", method =
RequestMethod.POST)). It contains service as an attribute with @Autowired Pour qu’un bean
soit injecté dans un attribute (injection de la dépendence) et éviter new().

Hint: if we have to use @RequestMapping("/edit/{id}"), then we have to use this too


public ModelAndView showEditStudentPage(@PathVariable(name = "id") int id){} . It
indicates that the method parameter should be bound to a URI template variable.

Hint: ModelAndView class merely holds both to make it possible for a controller to return
both model and view in a single return value. For that we specify the name of the html file in
its constructor (ex : ModelAndView modelAndView = new ModelAndView("edit");) and add
the model to it as an object (ex: modelAndView.addObject("student", student);).

4. Configure the project using application.properties file :


spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentcrud?
zerodatetimebehavior=Converttonull&useUnicode=true&useJDBCCompliantTimezoneShift=t
rue&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=

spring.jpa.hibernate.ddl-auto=update

server.error.whitelabel.enabled=false
server.port=8060
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false
5. Time for the front-end:
- First, html files should be in the templates folder and assets in static folder.
- We have to use the name space (xmlns:th=http://www.thymeleaf.org) it’s like taglibs:
. <a th:href="@{'/new'}"> : calls controller’s method that has new in the mapping
. <a th:href="@{'/edit/'+${student.id}}"> : like the previous, plus, it sends a variable to it
(see @PathVariable).
. <tr th:each="student:${studentlist}"> : to iterate all over a list
. <td th:text="${student.id}"> : to get an attribute value of an instance
. There is two syntaxes to accede to an attribute value of an instance:
 Dollar Syntax : ${student.id}
 Asterisk Syntax: where we have to select the object (instance) first by th:object =
"${student}" , then we accede to its attribute like this th:utext="*{id}" or
th:field="*{id}" (this syntax is usually used in forms).
Hint: th:utext="${student.id}" and th:utext="*{student.id}" are equivalent.

Docker
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
spring.datasource.continue-on-error=false
spring.datasource.generate-unique-name=false
spring.datasource.username=app

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

You might also like