CRUD in Spring Boot with Spring Data JPA

April 11, 2016 |

Tutorial about CRUD operations in Spring Boot with Spring Data JPA. Application developed in this tutorial performs adding, editing, updating and deleting contacts.

We will be using the following to successfully create our application in this tutorial:

  • Spring Web (Spring MVC, Spring Web MVC, a lot)
  • MySQL
  • Spring Data JPA (JPA, Hibernate)
  • Thymeleaf - template engine; bye JSP!
  • Many more, more than 50. With just a few lines in our pom.xml, let Spring Boot provide everything.

Create A Project

Go to Spring Initializr and create a Maven project.

Create Project in Spring Initializr

Once done, hit Generate Project button to save the project.

Unzip the project and start coding. You can use any editor or IDE you prefer. For Eclipse users, you will have to issue the following command via Terminal before importing to Eclipse:

$ cd springbootcrud
$ mvn eclipse:eclipse

For NetBeans users, just click File > Open Project... or click Open Project toolbar, and locate where you unzipped the project. NetBeans natively suports Maven project, no conversion needed.

Dependencies

Updadte your pom.xml:

Database Table

Create database in MySQL. Mine is springbootcrud. Add the following table:

Codes

Property

Insert the following to application.properties

Configuration

RepositoryConfiguration

Main Class

Application.java

Domain

Contact.java

Repository

ContactRepository.java

Service

ContactService.java

ContactServiceImpl.java

Controllers

HomeController.java

ContactController.java

Views

Fragments

templates/fragments/head.html

templates/fragments/header.html

templates/fragments/js.html

Home

templates/index.html

Contacts

templates/contact/index.html

templates/contact/add.html

templates/contact/view.html

templates/contact/edit.html

Static Files

CSS

static/csss/app.css

Also included are CSS (bootstrap.min.css, bootstrap-theme.min.css) and JavaScript (bootstrap.min) files of Bootstrap as well as jQuery. They can also be downloaded from Bootstrap website.

Running the Application

$ mvn package && java -jar target/springbootcrud-0.0.1.jar

Open your browser and point to: http://localhost:8080

Screenshots

Home Page

Create Project in Spring Initializr

Contacts Page

Contact List (without records)

Create Project in Spring Initializr

Add Contact Form

Create Project in Spring Initializr

Create Project in Spring Initializr

Contact List (with records)

Create Project in Spring Initializr

View Contact

Create Project in Spring Initializr

Edit Contact Form

Create Project in Spring Initializr

Please post your comments or suggestions.

You can download complete source code here.