Blog Detail

  • Home
  • Spring boot JPA Configuration

Spring boot JPA Configuration

In this blog I have posted how to do the JPA configuration in Spring boot

pom.xml

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

application.properties


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

spring.jpa.hibernate.ddl-auto=update

spring.jackson.serialization.order-map-entries-by-keys=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

Entity Class

package com.samplecoder.emo.entity;

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

@Entity
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	private String name;
	private int std;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getStd() {
		return std;
	}
	public void setStd(int std) {
		this.std = std;
	}
}

Repository class

package com.samplecoder.emo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.samplecoder.emo.entity.Student;

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer>{

}

Rest Controller

package com.samplecoder.emo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.samplecoder.emo.entity.Student;
import com.samplecoder.emo.repository.StudentRepository;

@RestController
@RequestMapping("/api/student")
public class StudentController {
	
	@Autowired
	private StudentRepository studentRepository;
	
	@PostMapping("/save")
	private Student save(@RequestBody Student student) {
		student = studentRepository.save(student);
		return student;
	}
	
	@DeleteMapping("/delete/{id}")
	private ResponseEntity<String> delete(@PathVariable("id") Integer id){
		studentRepository.deleteById(id);
		return new ResponseEntity<String>("DELTETED", HttpStatus.OK);
	}
	
	@GetMapping("/get/{id}")
	private Student get(@PathVariable(name = "id", required = true) Integer id) {
		return studentRepository.getById(id);
	}
	
	@GetMapping("/list")
	private List<Student> list(){
		return studentRepository.findAll();
	}
}

Test in Postman

Create

Request: http://localhost:8080/api/student/save
Body:
{
	"name":"Muthu",
	"std":10
}
Response:
{
	"id":1,
	"name":"Muthu",
	"std":10
}

Update

Request: http://localhost:8080/api/student/save
Body:
{
	"id":1,
	"name":"Muthu",
	"std":12
}
Response:
{
	"id":1,
	"name":"Muthu",
	"std":12
}

Get By ID

Request: http://localhost:8080/api/student/get/1
Response:
{
    "id": 1,
    "name": "Muthu",
    "std": 12,
    "hibernateLazyInitializer": {}
}

List All

Request: http://localhost:8080/api/student/list
Response:
[
    {
        "id": 1,
        "name": "Muthu",
        "std": 12
    }
]

Delete

Request: http://localhost:8080/api/student/delete/1
Response: DELTETED

1 Comment

Write a comment