You are currently viewing Complete CRUD Application with Spring Boot, Hibernate and MySQL

Complete CRUD Application with Spring Boot, Hibernate and MySQL

Hello guys, Bushan here, Welcome back to B2 Tech. Today in this article, you will learn how to build a complete CRUD application with Spring Boot, Hibernate, and MySQL. It is not a simple hello world application, it’s a fully-fledged end-to-end database application. You will learn to perform all the CRUD operations like CREATE, READ, UPDATE, and DELETE. So by the end of this article, you are able to create your own production-ready Spring boot application.

Before that let’s understand, What is Spring Boot and What are the benefits we will get over Spring MVC?
  • The first is that Boot is the “future of Spring”. That means that with Boot you can benefit from many commitments of the Spring community. Most of the Spring projects today are completely integrated with Boot, even the community starts to develop many applications based on Boot. For example for managing and monitoring.
  • With Spring Boot you can benefit from very nice and useful features such as an actuator and remote shell for managing and monitoring, which improves your application with production-ready features that are very useful.
  • Very nice and powerful properties and configuration controls – you can configure your application with and application.properties extend the boot in a very simple and impressive way, even the management in terms of overriding is very powerful.
  • It is one of the first micro-service ready platforms, and in my opinion, nowadays it is the best! Even if you don’t build a micro-service project with a boot you can benefit of using a modern approach in which you have an auto-consistent jar that can benefit from all the features that I described above or if you prefer you can impose the packaging as a classical war and deploy your war in any of the containers that you want.
  • Use of an intelligent and convention over configuration approach that reduces the startup and configuration phase of your project significantly. In fact, you have a set of starter Maven or Gradle dependencies that simplify dependency management. Then with the auto-configuration characteristic, you can benefit from a lot of standard configurations, that are introduced through the Conditional Configuration framework in Spring 4. You can override it with your specific configurations just defining your bean according to with the convention that you can see in the auto-configure JAR of the Boot dependency. Remember that Spring is open-source and you can see the code. Also the documentation, in my opinion, is good.

Spring initializer is a cool tool attainable at this link: https://start.spring.io/ is a very cool tool just to create your project in a very fast way.

What are we going to build in this article?

We are going to build a Book Management System, where the user can create a book, Read a book, Update a book, and Delete a book.

Cool, let’s get started.

NOTE: This is not a Spring JPA article, we are not using JPA, we will use Hibernate as an ORM framework.

Tools and Technologies used,
  • Java 1.8
  • Spring 2.0.5
  • Hibernate 5.1.0
  • MySQL
  • Eclipse Oxygen
  • JSTL taglib
  • JSP

First, let’s create Spring Boot starter project, go to https://start.spring.io/

Choose the maven project and choose the spring boot version which is 2.0.5 or the latest version. Enter the group id, artifact id, and dependencies as shown in the above image and then click on the Generate project. This will gives the Spring boot starter project with all the dependencies that we have mentioned above.

Open the project in Eclipse IDE, and open the pom.xml, and add the following dependencies.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur.springbootcrud</groupId>
	<artifactId>springbootcrudapi</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springbootcrudapi</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<start-class>in.bushansirgur.springbootcrud.springbootcrudapi.SpringbootcrudapiApplication</start-class>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Next, let’s create a Database and respective table for the application

To create a database, run this command

CREATE DATABASE crudapi;

To use this Database, run this command

USE crudapi;

To create a table, run this command

CREATE TABLE tbl_employee
(
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255),
    gender VARCHAR(255),
    department VARCHAR(255),
    dob DATE
);

The project configuration is pretty simple in Spring boot, we don’t have to maintain an XML file. So inside the application.properties file write the following code.

application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

spring.datasource.url=jdbc:mysql://localhost:3306/crudapi
spring.datasource.username=root
spring.datasource.password=

SpringbootcrudapiApplication.java

package in.bushansirgur.springbootcrud.springbootcrudapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootcrudapiApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootcrudapiApplication.class, args);
	}

}

Employee.java

package in.bushansirgur.springbootcrud.springbootcrudapi.model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "tbl_employee")
public class Employee {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column
	private Integer id;
	@Column
	private String name;
	@Column
	private String gender;
	@Column
	private String department;
	@Column
	private Date dob;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
				+ dob + "]";
	}
	
}

EmployeeDAOImpl.java

package in.bushansirgur.springbootcrud.springbootcrudapi.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import in.bushansirgur.springbootcrud.springbootcrudapi.model.Employee;

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

	@Autowired
	private EntityManager entityManager;
	
	@Override
	public List<Employee> get() {
		Session currentSession = entityManager.unwrap(Session.class);
		Query<Employee> query = currentSession.createQuery("from Employee", Employee.class);
		List<Employee> list = query.getResultList();
		return list;
	}

	@Override
	public Employee get(int id) {
		Session currentSession = entityManager.unwrap(Session.class);
		Employee employeeObj = currentSession.get(Employee.class, id);
		return employeeObj;
	}

	@Override
	public void save(Employee employee) {
		Session currentSession = entityManager.unwrap(Session.class);
		currentSession.saveOrUpdate(employee);
	}

	@Override
	public void delete(int id) {
		Session currentSession = entityManager.unwrap(Session.class);
		Employee employeeObj = currentSession.get(Employee.class, id);
		currentSession.delete(employeeObj);
	}

}

EmployeeDAO.java

package in.bushansirgur.springbootcrud.springbootcrudapi.dao;

import java.util.List;

import in.bushansirgur.springbootcrud.springbootcrudapi.model.Employee;

public interface EmployeeDAO {
	
	List<Employee> get();
	
	Employee get(int id);
	
	void save(Employee employee);
	
	void delete(int id);
}

@Entity annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable interface.

And why @Entity annotation is mandatory? … well, it is the way how JPA is designed. When you create a new entity you have to do at least two things

  1. annotated it with @Entity
  2. create an id field and annotate it with @Id

Anything else is optional, for example, the table name is derived from entity class name (and therefore @Table annotation can be optional), table’s columns are derived from entities variables (and therefore @Column annotation can be optional), and so on …

JPA is trying to provide a fast and easy start to developers who want to learn/use this API, and giving developers option to configure a few things as possible to make something functional is one of the ways how this API wants to achieve this “easy to use/learn” goal. Hence the @Entity annotation (together with @Id annotation) is the minimum you have to do in order to create an entity.

EmployeeService.java

package in.bushansirgur.springbootcrud.springbootcrudapi.service;

import java.util.List;

import in.bushansirgur.springbootcrud.springbootcrudapi.model.Employee;

public interface EmployeeService {
	List<Employee> get();
	
	Employee get(int id);
	
	void save(Employee employee);
	
	void delete(int id);
}

EmployeeServiceImpl.java

package in.bushansirgur.springbootcrud.springbootcrudapi.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.bushansirgur.springbootcrud.springbootcrudapi.dao.EmployeeDAO;
import in.bushansirgur.springbootcrud.springbootcrudapi.model.Employee;

@Service
public class EmployeeServiceImpl implements EmployeeService {

	@Autowired
	private EmployeeDAO employeeDAO;
	
	@Transactional
	@Override
	public List<Employee> get() {
		return employeeDAO.get();
	}

	@Transactional
	@Override
	public Employee get(int id) {
		return employeeDAO.get(id);
	}

	@Transactional
	@Override
	public void save(Employee employee) {
		employeeDAO.save(employee);
	}

	@Transactional
	@Override
	public void delete(int id) {
		employeeDAO.delete(id);
	}

}

@Repository: This is the Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. all the Database related operations are done by the repository.

EmployeeController.java

package in.bushansirgur.springbootcrud.springbootcrudapi.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import in.bushansirgur.springbootcrud.springbootcrudapi.model.Employee;
import in.bushansirgur.springbootcrud.springbootcrudapi.service.EmployeeService;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
		
	@RequestMapping(value = {"/", "/home", "/index"})
	public ModelAndView get() {
		ModelAndView mav = new ModelAndView("employeesList");
		List<Employee> list = employeeService.get();
		mav.addObject("list", list);
		return mav;
	}
	
	@RequestMapping("/openEmployeeView")
	public ModelAndView openEmployeeAddView() {
		ModelAndView mav = new ModelAndView("employeesAdd");
		mav.addObject("employee", new Employee());
		return mav;
	}
	
	@RequestMapping("/save")
	public ModelAndView save(@ModelAttribute("employee") Employee employeeObj) {
		ModelAndView mav = new ModelAndView("employeesList");
		employeeService.save(employeeObj);
		List<Employee> list = employeeService.get();
		mav.addObject("list", list);
		return mav;
	}
	
	@RequestMapping("/employee/{id}")
	public ModelAndView get(@PathVariable("id") int id) {
		ModelAndView mav = new ModelAndView("employeesAdd");
		Employee employeeObj = employeeService.get(id);
		if(employeeObj == null) {
			throw new RuntimeException("Employee not found"+id);
		}
		mav.addObject("employee", employeeObj);
		return mav;
	}
	
	@RequestMapping("/delete/{id}")
	public ModelAndView delete(@PathVariable("id") int id) {
		ModelAndView mav = new ModelAndView("employeesList");
		employeeService.delete(id);
		List<Employee> list = employeeService.get();
		mav.addObject("list", list);
		return mav;
	}
}

@Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user does not directly call the persistence method so it will call this method using this annotation. It will request @Repository as per user request

@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts like a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

employeesAdd.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>Add Employee</h1>
	<form:form action = "${pageContext.request.contextPath}/save" modelAttribute="employee">
		Enter Name: <form:input path="name"/><br/>
		Select Gender: <form:radiobutton path="gender" value = "Male"/>Male&nbsp;
					<form:radiobutton path="gender" value = "Female"/>Female&nbsp;<br/>
		Departement: 
		<form:select path="department">
			<form:option value="Support">Support</form:option>
			<form:option value="Testing">Testing</form:option>
			<form:option value="Development">Development</form:option>
			<form:option value="Business Analyst">Business Analyst</form:option>
		</form:select><br/>
		Date of Birth: <form:input path="dob" type = "date"/><br/>
		<form:hidden path = "id"/>
		<button type = "submit">Save</button>
	</form:form>
</body>
</html>

employeesList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>List of Employees</h1>
	<table border = "1">
		<tr>
			<th>Name</th>
			<th>Gender</th>
			<th>Department</th>
			<th>Date of Birth</th>
			<th>Actions</th>
		</tr>
		<c:forEach items="${list}" var="e">
			<tr>
				<td>${e.name}</td>
				<td>${e.gender}</td>
				<td>${e.department}</td>
				<td>${e.dob}</td>
				<td>
					<a href = "${pageContext.request.contextPath}/employee/${e.id}">Edit</a>
					|
					<a href = "${pageContext.request.contextPath}/delete/${e.id}">Delete</a>
				</td>
			</tr>
		</c:forEach>
	</table>
	<button onclick="window.location.href='${pageContext.request.contextPath}/openEmployeeView'">Add Employee</button>
</body>
</html>

Now right click on the project, choose run as and click maven clean

Now again right-click on the project, choose run as and click maven build,

It will ask us to enter the goals, enter ‘package‘ and click run

Once the project has build successfully, open the main class SpringbootcrudapiApplication.java and run the file as a java application, the final application looks like this,

That’s it for this article, i hope this article is helped you in one or the other way if so then please share this article with all social media profiles. If you have any queries regarding this article/post then leave your comments in the comment section, I will try to answer for your queries. Thank you so much for reading, I will see you in the next article.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

This Post Has 7 Comments

  1. Simon Temesgen

    Your tutorial helped me alot,
    Thank you very much for you support!

  2. Simon Temesgen

    Dear Bz Tech,
    Edit(Update) operation is not working on my side,could you provide support on that .
    Thank you!
    Simon T

  3. NARAYAN

    Thanks for this application of CRUD MVC with Spring Boot. I want same application with REST and WEB MVC with Spring Boot. How to handle RequestMapping PUT and DELETE methods for access both methods using JSP and POSTMAN.

  4. pallavi

    pagination with spring MVC , please help us

  5. emily

    source code?

  6. Shankarachary K

    need source code.

  7. Sandip

    You said that you are going to build a project related to book, but built a project about employee management.!!!!

Leave a Reply to NARAYAN Cancel reply