Spring Boot JPA

Spring Boot JPA with features, project, starter project wizard, cli, application, annotations, dm, properties, actuator, thymeleaf view, jpa, jdbc

Spring Boot JPA

Spring Boot JPA

Spring Boot provides spring-boot-starter-data-jpa starter to connect Spring application with relational database efficiently. You can use it into project POM (Project Object Model) file.

Here, we are creating a spring-boot application which uses JPA to connect to the database.

Spring Boot provides default server and apache derby database. So, we will use that in our application.

Now, let's create project. Follow the following steps.

Select project type

Provide project name.

Select dependencies

After finishing, create the following files.

// pom.xml

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
  2. <projectxmlnsprojectxmlns="https://maven.apache.org/POM/4.0.0"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.javatpoint</groupId>  
  6.     <artifactId>spring-boot-crud</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.     <name>spring-boot-crud</name>  
  10.     <description>spring boot application for crud</description>  
  11.     <parent>  
  12.         <groupId>org.springframework.boot</groupId>  
  13.         <artifactId>spring-boot-starter-parent</artifactId>  
  14.         <version>2.0.0.BUILD-SNAPSHOT</version>  
  15.         <relativePath/><!-- lookup parent from repository -->  
  16.     </parent>  
  17.     <properties>  
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  20.         <java.version>1.8</java.version>  
  21.     </properties>  
  22.   
  23.     <dependencies>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-data-jpa</artifactId>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>org.springframework.boot</groupId>  
  30.             <artifactId>spring-boot-starter-web</artifactId>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.apache.derby</groupId>  
  34.             <artifactId>derby</artifactId>  
  35.             <scope>runtime</scope>  
  36.         </dependency>  
  37.         <dependency>  
  38.             <groupId>org.springframework.boot</groupId>  
  39.             <artifactId>spring-boot-starter-test</artifactId>  
  40.             <scope>test</scope>  
  41.         </dependency>  
  42.             <dependency>  
  43.         <groupId>org.springframework.boot</groupId>  
  44.         <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  45.     </dependency>  
  46.     </dependencies>  
  47.     <build>  
  48.         <plugins>  
  49.             <plugin>  
  50.                 <groupId>org.springframework.boot</groupId>  
  51.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  52.             </plugin>  
  53.         </plugins>  
  54.     </build>  
  55.     <repositories>  
  56.         <repository>  
  57.             <id>spring-snapshots</id>  
  58.             <name>Spring Snapshots</name>  
  59.             <url>https://repo.spring.io/snapshot</url>  
  60.             <snapshots>  
  61.                 <enabled>true</enabled>  
  62.             </snapshots>  
  63.         </repository>  
  64.         <repository>  
  65.             <id>spring-milestones</id>  
  66.             <name>Spring Milestones</name>  
  67.             <url>https://repo.spring.io/milestone</url>  
  68.             <snapshots>  
  69.                 <enabled>false</enabled>  
  70.             </snapshots>  
  71.         </repository>  
  72.     </repositories>  
  73.     <pluginRepositories>  
  74.         <pluginRepository>  
  75.             <id>spring-snapshots</id>  
  76.             <name>Spring Snapshots</name>  
  77.             <url>https://repo.spring.io/snapshot</url>  
  78.             <snapshots>  
  79.                 <enabled>true</enabled>  
  80.             </snapshots>  
  81.         </pluginRepository>  
  82.         <pluginRepository>  
  83.             <id>spring-milestones</id>  
  84.             <name>Spring Milestones</name>  
  85.             <url>https://repo.spring.io/milestone</url>  
  86.             <snapshots>  
  87.                 <enabled>false</enabled>  
  88.             </snapshots>  
  89.         </pluginRepository>  
  90.     </pluginRepositories>  
  91. </project>  

// UserRecord.java

  1. package com.tpoint;  
  2. import javax.persistence.Entity;  
  3. import javax.persistence.Id;  
  4. @Entity  
  5. public class UserRecord {  
  6.     @Id  
  7.     private intid;  
  8.     private String name;  
  9.     private String email;  
  10.     public UserRecord(){}  
  11.     public int getId() {  
  12.         returnid;  
  13.     }  
  14.     public void setId(intid) {  
  15.         this.id = id;  
  16.     }  
  17.     public String getName() {  
  18.         returnname;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     public String getEmail() {  
  24.         returnemail;  
  25.     }  
  26.     public void setEmail(String email) {  
  27.         this.email = email;  
  28.     }  
  29. }  

// UserService.java

  1. package com.tpoint;  
  2. import java.util.List;  
  3. import java.util.Optional;  
  4. import java.util.ArrayList;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Service;  
  7. @Service  
  8. public class UserService {  
  9.     @Autowired  
  10.     private UserRepository userRepository;  
  11.     public List<UserRecord> getAllUsers(){  
  12.         List<UserRecord>userRecords = new ArrayList<>();  
  13.         userRepository.findAll().forEach(userRecords::add);  
  14.         returnuserRecords;  
  15.     }  
  16.     public Optional<UserRecord> getUser(String id){  
  17.         returnuserRepository.findOne(id);  
  18.     }  
  19.     public void addUser(UserRecord userRecord){  
  20.         userRepository.save(userRecord);  
  21.     }  
  22.     public void delete(String id){  
  23.         userRepository.delete(id);  
  24.     }  
  25.  

//UserController.java

  1. package com.tpoint;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RequestBody;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8. import java.util.List;  
  9. import java.util.Optional;  
  10. @RestController  
  11. public class UserController {  
  12.     @Autowired  
  13.     private UserService userService;   
  14.     @RequestMapping("/")  
  15.     public List<UserRecord> getAllUser(){  
  16.         returnuserService.getAllUsers();  
  17.     }     
  18.     @RequestMapping(value="/add-user", method=RequestMethod.POST)  
  19.     public void addUser(@RequestBody UserRecord userRecord){  
  20.         userService.addUser(userRecord);  
  21.     }  
  22.     @RequestMapping(value="/user/{id}", method=RequestMethod.GET)  
  23.     public Optional<UserRecord> getUser(@PathVariable String id){  
  24.         returnuserService.getUser(id);  
  25.     }  
  26. }  

// UserRepository.java

  1. package com.tpoint;  
  2. import org.springframework.data.repository.CrudRepository;  
  3. public interface UserRepository extends CrudRepository<UserRecord, String> {  
  4. }  

// SpringBootJpaApplication.java

  1. package com.point;  
  2. import org.springframework.boot.SpringApplication;  
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  4. @SpringBootApplication  
  5. public class SpringBootJpaApplication {  
  6.     public static void main(String[] args) {  
  7.         SpringApplication.run(SpringBootJpaApplication.class, args);  
  8.     }  
  9.  

Our project looks like the following.

Now, run the application.

Here, we are using Postman plug-in of the Firefox browser to test our application. Postman is a nice tool which provides interface to request HttpRequests. You can download it from the official site of Firefox.


Opening Postman

Providing URL on which application is running. After that selecting HttpRequest method.

Submit the request.

After submitting, it returns a blank array as response because initially there is no data into the database.

Adding data into the repository.

Providing a new URL for adding user and data to insert. Select POST method and submit.

After submitting, it returns response code 200 which means data has inserted successfully.

Now, fetch the inserted record by changing the request URL.

Notice that after submitting, it returns inserted data in response box.

Well, we have created Spring Boot application by using JPA successfully.