Spring Boot Restful CRUD application with Hibernate, MySQL database
Tools used :
- Java 8 or above
- Spring Tool Suite 4 ( or Eclipse)
- Spring Boot 2.4.5
Created a Spring Starter Project using STS, which under the hood redirects to Spring Initializer. Following starter dependencies will create a skeleton project that includes all the relevant JARs to build a Restful web application with MySQL database.
- Spring web: for spring MVC
- Spring Data JPA
- MySQL Driver: to establish database connection
- Lombok: to avoid repetitive code (getters, setters, constructors, etc..)
Here's how the pom.xml would look like after adding all the above dependencies. spring-boot-maven-plugin allows you to package executable jar or war archives and run the application with the embedded server.
Since we are going to create a BookStore Application, our Model class would be Book.java where we should provide Entity annotation for the class. Entities in JPA are nothing but POJOs representing data that can be persisted in the database. An entity represents a table stored in a database. Also creating getters and setters will be handled by the Lombok dependency itself.
The next step would be creating the BookRepository interface which extends JpaRepository. JpaRepository contains the full API of CrudRepository and PagingAndSortingRepository. So it contains basic CRUD operations and also API for pagination and sorting. Also, note that JpaRepository provides the type of the Entity and data type of its primary key as in Figure 3.
Since we are creating a spring boot application, we can easily skip coding for establishing the database connection. Instead, add all the data source-related properties and hibernate-related properties to the application.properties file (or YAML). bookstore would be the database name that you created in the MySQL database.
Use the following SQL file to create a new database using MySQL Workbench.
As it is a good practice to define a service layer, came up with the BookService class which is used to write business logic in a different layer, separated from the @RestController class file. Service Component class files are annotated with @Service as in below. It has all the CRUD operations defined by those functions that relevant to our application.
@RestController is a specialized version of the controller. It includes both @Controller and @ResponseBody annotations, which as a result simplifies the controller implementation. Every request handling method of the controller class automatically serializes the return objects into HttpResponse.
All the annotations which you came through in the BookController class are explained below.
@PostMapping: for mapping HTTP POST requests onto specific handler methods.
@GetMapping: for mapping HTTP GET requests onto specific handler methods.
@DeleteMapping: for mapping HTTP DELETE requests onto specific handler methods.
@PutMapping: for mapping HTTP PUT requests onto specific handler methods.
@RequestBody: relevant method parameter should be bound to the body of the web request.
@PathVariable: method parameter should be bound to a URI template variable.
Now we run our Main class, SpringBootMysqlDemoApplication.java to check whether the application works accordingly. Peculiarly for POST and PUT methods, we need a REST client like Postman to send an HTTP request with the post body. Therefore refer to the screenshots below to analyze the HTTP responses correspond to their API endpoints.
POST request Url: http://localhost:8080/createBooks
GET request Url: http://localhost:8080/retrieveBooksByAuthor/Leo Tolstoy
DELETE request Url: http://localhost:8080/deleteBook/11
PUT request Url: http://localhost:8080/updateBook
Hope you find this article helpful to create a simple BookStore Application, which is basically a spring boot CRUD web application with the MySql database. The complete source code of this application can be accessed via GitHub.