Simple Spring Boot CRUD Web Application with Spring MVC, Spring Data JPA and H2 Database
Tools used :
- Java 8 or above
- Spring Tool Suite 4 ( or Eclipse)
- Spring Boot 2.3.2
Start by creating a new spring boot application, and following are the dependencies you have to choose for a bare minimum project.
- Spring web (for spring MVC)
- Spring Data JPA
- H2 Database
Have a glance at how pom.xml file would look after adding all the relevant dependencies.
After spring starter project is created my first move is to come up with the Model class, which is "JournalEntry" in my case. Add all the fields, getters and setters as in the Figure 2.
According to the Spring MVC concept, now we gonna focus on the View , which are the .jsp files in this application. I have created all the .jsp pages under the webapp folder, which is the typical location to place it. But in case you want to change the folder location, add the path to the properties file. In home.jsp, addEntry is to "create" data, deleteEntry is to "delete" data from the database and all the other actions, are to "read" information from the database. Since read/retrieved information should be displayed, Figure 4 and 5 .jsp files have been created. Apart from those two, I have added getEntriesByCategory.jsp, getEntriesByCategorySorted.jsp and getEntriesByIdGT.jsp in my application which has the same code as in Figure 5, to retrive the data according to the the relevant HTTP requests accordingly.
Next step is to add the application.properties file to enable H2 database to access it. To access H2 console type http://localhost:8080/h2-console in the browser.
You can insert data to the database either,
- from using a data.sql file in the application or
- through h2-console or
- simply through the home.jsp interface
Therefore to enter data to the database through home.jsp interface, you have to add a controller with a specific route. Following is the controller class which is responsible for returning the relevant .jsp for a specific HTTP request with an added action.
In here you can see there's a reference object repo from JournalEntry Repository interface. That object is used to call the built in methods from JpaRepository class, such as save(), findAll(), findById(), deleteById().
Apart from the basic CRUD operations, complex queries such as findByCategory(), findByIdGreaterThan(), findByCategorySorted() can be used in the application very easily. You just need to follow simple naming conventions and the function is ready to go. (As in the Figure 8 )
In case you want to insert bulk data to the application at once for the testing purposes of retriveing information following data.sql is the way to go. Add data.sql file under src/main/resources, right where the application.properties file is placed.
Finally you can run the application. An important fact to be noted here is that each spring boot web application includes an embedded web server. The advantage of having this is, that you don't need the server pre-installed in the deployment environment. With spring boot, default embedded server is Tomcat. Other options available are Jetty and UnderTow.
In this article we looked at the basic CRUD operations that can be done in the spring boot web application environment. The complete source code can be found for this tutorial on GitHub .