Spring Boot Tutorial-Spring Data JPA Simple Example
In this post we create a Employee management system which adds and retrieves employee details.
Lets Begin
We make use of the h2 database. Maven will be as follows-
<?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>
<groupId>com.EXP</groupId>
<artifactId>SpringBootHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootHelloWorld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</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>
package com.EXP;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
Create the Entity class as follows-
package com.EXP.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
private String dept;
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";
}
}
package com.EXP.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.EXP.data.EmployeeRepository;
import com.EXP.model.Employee;
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepository employeeData;
@RequestMapping(value = "/addNewEmployee.html", method = RequestMethod.POST)
public String newEmployee(Employee employee) {
employeeData.save(employee);
return ("redirect:/list.html");
}
@RequestMapping(value = "/addNewEmployee.html", method = RequestMethod.GET)
public ModelAndView addNewEmployee() {
Employee emp = new Employee();
return new ModelAndView("newEmployee", "form", emp);
}
@RequestMapping(value = "/listEmployees.html", method = RequestMethod.GET)
public ModelAndView employees() {
List<Employee> allEmployees = employeeData.findAll();
return new ModelAndView("allEmployees", "employees", allEmployees);
}
}
Next we define the EmployeeRepository which is an interface that extends the Spring Framework class JpaRepository. JpaRepository class is a generics and takes the following two parameters as arguments-
- What type of Object will this repository be working with- In our case Employee
- Id will be what type of object- Long(since id defined in the Employee class is long)
package com.EXP.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.EXP.model.Employee;
public interface EmployeeRepository extends JpaRepository<Employee
, Long> {
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <h1>Employees page</h1> <ul> <c:forEach items="" var="employee"> <li></li> </c:forEach> </ul>
Define the allEmployees.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
<h1>Add new employee</h1>
<form:form modelAttribute="form">
<form:errors path="" element="div" />
<div>
<form:label path="name">Name</form:label>
<form:input path="name" />
<form:errors path="name" />
</div>
<div>
<input type="submit" />
</div>
</form:form>
</body>
</html>
The application.properties will be as follows-
spring.mvc.view.prefix:/WEB-INF/jsp/ spring.mvc.view.suffix:.jsp spring.datasource.url=jdbc:h2:file:./DB spring.jpa.properties.hibernate.hbm2ddl.auto=update
Compile and the run the SpringBootHelloWorldApplication.java as a Java application.
Go to localhost:8080/addNewEmployee.html
Click add. We will be directed to the allEmployees.jsp
No comments:
Post a Comment