Java and PostgreSQL web Application PART 5
Introduction
This tutorial is the fifth part in a series explaining how to create a JAVA PostgreSQL web application with various databases, including ECLIPSE, Spring, Hibernate and Tomcat. This series is designed to help build a web application, from scratch, that will interact with these databases. Part five of this series will cover some of the code needed to make the application possible, including the Java PostgreSQL Web Application repository interface. Be sure to have a thorough understanding of the previous four sections of this tutorial before proceeding.
Prerequisites
- A solid grasp on the first four sections of this tutorial. Following are the links to sections one through four:
- Java and PostgreSQL web Application PART 1
- Java and PostgreSQL web Application PART 2
- Java and PostgreSQL web Application PART 3
- Java and PostgreSQL web Application PART 4
Coding the Java and PostgreSQL Web Application’s Repository Interface
This tutorial will explore the repository interface of the web application. Specifically, it will examine the CrudRepository
interface described by Spring Data.
Looking back, the first part of this tutorial provided an “Architecture” diagram of the application. Here the data-access layer of the application, and the repository, will be configured.
First, a package for the interface must be created. Right click on the “src” folder and then select “New.” Now select “Package” to open the new JAVA package window. As shown in the following image, the package is named “com.chinito.carsmanager.dao”:
After creating the package, right click on the package and then select “New.” Now, as shown in the following screenshot, select “Interface,” and then provide the name CarsRepository
for the interface:
Now click the “Finish” button to create the interface.
The JAVA code for the interface follows:
1 2 3 4 5 6 7 8 9 10 | package com.chinito.carsmanager.dao; import org.springframework.data.repository.CrudRepository; import com.chinito.carsmanager.model.Cars; public interface CarsRepository extends CrudRepository<cars, integer="Integer">{ } |
Note the above code is almost none. This is because the Data Access Object, or DOA, is not coded in Spring Data JPA.
Here the code simply declares the CarsRepository
interface and then extends the CrudRepository
interface. This is done to define the CRUD methods, as follows:
- findAll()
- deletedById()
- findById()
- save()
- etc.
It is important to note that the implementation of codes are automatically generated at runtime by the Spring Data JPA. The type of the primary key field must be specified whenever the interface CrudRepository
is extended. In this case it would be CrudRepository<cars, ineteger="Ineteger">
.
Coding the Java and PostgreSQL Web Application’s Service Class
With the repository interface of the web application now created, the service class of the web application must be coded.
First, a service class must be created inside the same package as the repository interface and given a meaningful name. Now click the “Finish” button, as shown below, to create the JAVA class:
The code used for the business logic follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.chinito.carsmanager.dao; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.chinito.carsmanager.model.Cars; @Service public class CarsService { @Autowired private CarsRepository carsRepo; public List<cars> listAll(){ return (List<cars>) carsRepo.findAll(); } public void save(Cars cars) { carsRepo.save(cars); } public Cars get(Integer id) { Optional<cars> result = carsRepo.findById(id); return result.get(); } public void delete(Integer id) { carsRepo.deleteById(id); } } |
Here is a breakdown of the above code:
The @Service annotation is used to clearly specify the intent of this class after the method has been detected by Spring Data JPA.
The @Autowired CarsRepository carsRepo
is used to handle the automatic generation of the implementation code at runtime. This code simply assigns the function call to the CarsRepository object carsRepo
.
While this class may seem redundant, it is essential for the application to decouple the DAO layer and the service layer.
Coding the Java and PostgreSQL Web Application’s Controller Class
This section will review the controller class of the web application that was created in the second part of this tutorial series, located here: Java and PostgreSQL web Application PART 2.
Remember that this controller class will handle any and all requests coming from the users.
The new code for the class controller follows:
1 2 3 4 5 6 7 8 9 | @Controller public class CarsController { @Autowired private CarsService service; // All the handler will go here } |
This is the basic form of a controller class in Spring MVC. The class is annotated using the
@Controller
annotation.The annotation @Autowired is used to inject the
CarService
into this class. The succeeding code will be the handler methods that will enable the features demonstrated in the first section of this tutorial.
Conclusion
This tutorial was the fifth part in a series explaining how to create a JAVA PostgreSQL web application with several different databases. This article explained some of the code needed to make the application’s function possible, specifically the coding the Java and PostgreSQL web app’s repository interface, the web application’s service and controller classes and a broken-down explanation of the code. It is important to remember that the DOA is not coded in Spring Data JPA. The next part of this tutorial series will cover the creation of the handler methods.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started