CRUD operations using Spring Boot + JPA + Hibernate + PostgreSQL
I’ll start this tutorial by keeping in mind that you already know the basics of Spring Boot.
If you are not familiar with Spring Boot I strongly recommend you to look at the following tutorial Which explains you the basics of Spring Boot. It explains you how to create your first spring boot application and how to add dependencies and how to make your first app up and running.
https://onecompiler.com/posts/3snmv3hfd/how-to-create-a-website-with-spring-boot-and-serve-static-content
Dependencies & Configurations
Let’s jump into the matter, First thing you need to do is create the Spring Boot application and add the Spring Boot , Spring Data JPA and PostgreSQL dependencies to your build.gradle file
compile 'org.springframework.boot:spring-boot-starter-web:1.5.6.RELEASE' compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE' compile 'org.postgresql:postgresql:42.1.4'
Now your build.gradle file should look like this
apply plugin: 'java-library' apply plugin: 'java' apply plugin: 'eclipse' repositories < jcenter() >dependencies
After adding these dependencies do «Refresh Gradle Project» so that Gradle downloads all the required dependencies for you.
Now add the database name to application.properties file, for this tutorial i am keeping my database name as example_db and the user as example_user
spring.datasource.driverClassName=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/example_db spring.datasource.username=example_user spring.datasource.password=password spring.jpa.generate-ddl=true
Repository & Controller
Create your main program which starts the Spring Boot application
com.example.Start.java
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Start < public static void main(String[] args) < SpringApplication.run(Start.class, args); >>
Create a User model pojo with the fields needed for User operations with setters & getters.
com.example.model.User
package com.example.example.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Parameter; @Entity @Table(name = "users") public class User < @GenericGenerator( name = "usersSequenceGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = < @Parameter(name = "sequence_name", value = "usersSequence"), @Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "1") >) @Id @GeneratedValue(generator = "usersSequenceGenerator") private long id; private String name; private int age; private String email; public long getId() < return id; >public void setId(long id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public int getAge() < return age; >public void setAge(int age) < this.age = age; >public String getEmail() < return email; >public void setEmail(String email) < this.email = email; >>
Now Create UserRepository Interface
com.example.repository.UserRepository
package com.example.example.repository; import java.util.List; import org.springframework.data.repository.CrudRepository; import com.example.example.model.User; public interface UserRepository extends CrudRepository < ListfindByName(String lastName); >
Now create the controller with Create, Read, Update & Delete REST endpoints
com.example.controller.UserController
package com.example.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; 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 com.example.example.model.User; import com.example.example.repository.UserRepository; @RestController @RequestMapping("/user") public class UserController < @Autowired UserRepository userRepository; @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public void create(@RequestBody User user) < userRepository.save(user); >@RequestMapping(value = "/") public User read(@PathVariable long id) < return userRepository.findOne(id); >@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public void update(@RequestBody User user) < userRepository.save(user); >@RequestMapping(value = "/", method = RequestMethod.DELETE) public void delete(@PathVariable long id) < userRepository.delete(id); >>
Now you have all the Create, Read, Update & Delete operations enabled on the User collection.
Let’s start testing them.
You may want to use a REST explorer to test these REST endpoints my favorite is Postman in this tutorial I’ll show you the curl commands so that you can simply execute them from the command line.
Testing the REST endpoints
1. Create
If you run the above curl command from command prompt that will create the user row in example_db database.
2. Read
GET /user/1 HTTP/1.1 Host: localhost:8080
curl -X GET \ http://localhost:8080/user/1 \
3. Update
4. Delete
DELETE /user/1 HTTP/1.1 Host: localhost:8080
curl -X DELETE \ http://localhost:8080/user/1 \