0% found this document useful (0 votes)
6 views

Spring Data Jpa

Uploaded by

gokulpradeep1908
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Spring Data Jpa

Uploaded by

gokulpradeep1908
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Spring Data Jpa:

It is the abstract layer of spring project database.It has two important


interface
that is CrudRepository and JpaRepository.Using this interfaces we can perform CRUD
Operations in spring projects.Spring data jpa provides EntityManager interface to
write a Sql queries. JPA (Java Persistence Api).

1.CrudRepository:

It is a interface which is used to perform basic CRUD operation.It provides


basic CRUD method eg(save(),findAll(),findById(),deleteAll()).It does not support
finder methods means (own query) and advance methods.

2.JpaRepository:

It is a interface which is used to perform basic and advance CRUD operations.


It provides finder methods and advance methods. It extends CrudRepository so only
we can use basic and finder methods.

Annotations:

1.@Entity - It is a stereo type annotation which is used to create a table.


2.@Id - It is a field level annotation which is used to set a primary key
column for table
3.@Table - It is a class level annotation which is used to set a user defined
table name otherwise it takes class name as a table name.
4.@Column - It is a field level annotation which is used to set a user
defined column name otherwise it takes field name as a column name.
5.@GeneratedValue - It is used to set a auto increment value for a column
6.@Repository - It is stereo type annotation which is used to denote the
class as a repository class.

Program:

application.properties file:

spring.application.name=StudentCRUD
spring.datasource.url=jdbc:mysql://localhost:3306/StudentDetails
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update

Controller class:

package com.uniq.StudentCRUD.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import com.uniq.StudentCRUD.entity.Student;
import com.uniq.StudentCRUD.service.StudentService;

@RestController
@RequestMapping("/api")
public class StudentController {

@Autowired
StudentService service;

@PostMapping("/postdet")
public String postDetails(@RequestBody Student student) {

service.addDetails(student);
return "Add Successfully";

Service Class:

package com.uniq.StudentCRUD.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uniq.StudentCRUD.entity.Student;
import com.uniq.StudentCRUD.repository.StudentRepository;

@Service
public class StudentService {

@Autowired
StudentRepository repository;

public void addDetails(Student student) {

repository.save(student);

Repository Layer:

package com.uniq.StudentCRUD.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.uniq.StudentCRUD.entity.Student;

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

Dependencies:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

You might also like