spring boot jpa examples
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.
// pom.xml
<?xmlversionxmlversion="1.0"encoding="UTF-8"?>
<projectxmlnsprojectxmlns="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.javatpoint</groupId>
<artifactId>spring-boot-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-crud</name>
<description>spring boot application for crud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</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-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
// UserRecord.java
package com.javtpoint;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
publicclass UserRecord {
@Id
privateintid;
private String name;
private String email;
public UserRecord(){}
publicint getId() {
returnid;
}
publicvoid setId(intid) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getEmail() {
returnemail;
}
publicvoid setEmail(String email) {
this.email = email;
}
}
// UserService.java
package com.javtpoint;
import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
publicclass UserService {
@Autowired
private UserRepository userRepository;
public List<UserRecord> getAllUsers(){
List<UserRecord>userRecords = new ArrayList<>();
userRepository.findAll().forEach(userRecords::add);
returnuserRecords;
}
public Optional<UserRecord> getUser(String id){
returnuserRepository.findOne(id);
}
publicvoid addUser(UserRecord userRecord){
userRepository.save(userRecord);
}
publicvoid delete(String id){
userRepository.delete(id);
}
}
//UserController.java
package com.javtpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
publicclass UserController {
@Autowired
private UserService userService;
@RequestMapping("/")
public List<UserRecord> getAllUser(){
returnuserService.getAllUsers();
}
@RequestMapping(value="/add-user", method=RequestMethod.POST)
publicvoid addUser(@RequestBody UserRecord userRecord){
userService.addUser(userRecord);
}
@RequestMapping(value="/user/{id}", method=RequestMethod.GET)
public Optional<UserRecord> getUser(@PathVariable String id){
returnuserService.getUser(id);
}
}
// UserRepository.java
package com.javtpoint;
import org.springframework.data.repository.CrudRepository;
publicinterface UserRepository extends CrudRepository<UserRecord, String> {
}
// SpringBootJpaApplication.java
package com.javtpoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJpaApplication.class, args);
}
}
Now run the applications
REFERENCE
SPRING BOOT JPA EXAMPLE
SPRING OFFICIAL