Friday, March 29, 2024
HomeJavaSpring Boot + Hibernate Tutorial and Instance for Java Builders

Spring Boot + Hibernate Tutorial and Instance for Java Builders


bundle service;

import com.scholar.crudapp.mannequin.Scholar;
import com.scholar.crudapp.repository.StudentRepository;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Checklist;

@Service
@Transactional
public class StudentService {

@Autowired
StudentRepository studentRepository;

//Get all the scholars
public Checklist<Scholar> getAllStudents() {
Checklist<Scholar> college students = studentRepository.findAll();
return college students;
}

//show one scholar by id
public Scholar getStudentById(int id) {
return studentRepository.findById(id);
}

//save scholar in database
public void saveStudent(Scholar scholar) {
attempt{
studentRepository.save(scholar);
}
catch(Exception e){
e.printStackTrace();
}
}

//delete stuednt by id
public void deleteStudent(int id) {
attempt{
studentRepository.deleteById(id);
}catch(Exception e){
e.printStackTrace();
}
}

}







      
      
      

5. Controller Class 

That is the category which is coping with RESTful APIs for CRUD operations.
bundle com.scholar.crudapp.controller;

import com.scholar.crudapp.mannequin.Scholar;
import com.scholar.crudapp.repository.StudentRepository;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.net.bind.annotation.*;

import java.util.Checklist;

@Controller
public class StudentController {

@Autowired
StudentRepository studentRepository;

//verify the api's working accurately api
@RequestMapping(worth="/ping", methodology=RequestMethod.GET)
@ResponseBody
public String healthCheck() {
return "That is working effectively";
}

@RequestMapping(worth="/college students", methodology=RequestMethod.GET)
@ResponseBody
public Checklist<Scholar> getAllStudents() {
return studentRepository.findAll();
}

@RequestMapping(worth="/scholar", methodology=RequestMethod.POST)
@ResponseBody
public Scholar addStudent(Scholar scholar) {
return studentRepository.save(scholar);
}

@RequestMapping(worth="/findstudent", methodology = RequestMethod.GET)
@ResponseBody
public Scholar findStudent(@RequestParam("studentId") int studentId) {
return studentRepository.findById(studentId);
}

@RequestMapping(worth= "/updatestudent", methodology = RequestMethod.GET)
@ResponseBody
public Scholar updateStudent(@RequestBody Scholar scholar){
return studentRepository.save(scholar);
}

@RequestMapping(worth="/deletestudent", methodology = RequestMethod.GET)
@ResponseBody
public int deleteStudent(@RequestParam("studentId") int studentId) {
return studentRepository.deleteById(studentId);
}
}


Right here, the @Controller annotation is used to reveal the RESTful APIs. The remaining controller nonetheless takes benefit of the spring's dependency injection.

Hibernate utilized in Spring

As you see within the above mannequin class, Scholar.java we used the tables names in higher case letters. As we already know, by default hibernate will generate the names of the tables in lowercase letters.

@Entity
@Desk(identify = "STUDENT")
public class Scholar {
    //your code right here
}

However this won't work until you setting this property in software.properties file. This can can help you create your tables names in database in higher case.
spring.jpa.hibernate.naming.physical-strategy
  =org.hibernate.boot.mannequin.naming.PhysicalNamingStrategyStandardImpl

When ought to we use Hibernate together with Spring and likewise what's the benefit?

While you use spring, you employ IOC to resolve the 'plumbing' code that connects the assorted software layers. Consequently, Spring capabilities as a middleware, permitting another suitable know-how to entry your knowledge layer via a context with out requiring specific knowledge dependencies. 

The hibernate configuration will likely be affected by adjustments in your database, however you may specify what number of adjustments you wish to translate into your software. I would not advocate these frameworks for actually excessive efficiency/esoteric functions as a result of they had been constructed with enterprise software necessities and design ideas in thoughts.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments