Simple Spring Boot CRUD Web Application with Spring MVC, Spring Data JPA and H2 Database

Tools used :

Start by creating a new spring boot application, and following are the dependencies you have to choose for a bare minimum project.

Have a glance at how pom.xml file would look after adding all the relevant dependencies.

Figure 1 : pom.xml

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.

Figure 2 : JournalEntry.java

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.

Figure 3 : home.jsp
Figure 4 : getEntry.jsp
Figure 5 : getAllEntries.jsp

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.

Figure 6 : application.properties

You can insert data to the database either,

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.

Figure 7 : JournalEntryController.java

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 )

Figure 8 : JournalEntryRepository.java

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.

Figure 9 : data.sql

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.

Figure 10 : SpringBootJournalAppApplication.java

Conclusion :

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 .

Technology Enthusiast, Software Engineer, Java Lover