Spring Boot Restful CRUD application with Hibernate, MySQL database

Tools used :

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.

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.

Figure 1: pom.xml

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.

Figure 2: Book.java

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.

Figure 3: BookRepository.java

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.

Figure 4: application.properties

Use the following SQL file to create a new database using MySQL Workbench.

Figure 5: db.sql

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.

Figure 6: BookService.java

@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.

Figure 7: BookController.java

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

Conclusion :

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.

GitHub: https://github.com/vgunawardana/spring-boot-mysql-app

Technology Enthusiast, Software Engineer, Java Lover