English 中文(简体)
Spring Boot & H2 - Project Setup
  • 时间:2024-12-22

Spring Boot & H2 - Project Setup


Previous Page Next Page  

As in previous chapter Environment Setup, we ve imported the generated spring boot project in ecppse. Now let s create the following structure in src/main/java folder.

Project Structure

    com.tutorialspoint.controller.EmployeeController − A REST Based Controller to implement REST based APIs.

    com.tutorialspoint.entity.Employee − An entity class representing the corresponding table in database.

    com.tutorialspoint.repository.EmployeeRepository − A Repository Interface to implement the CRUD operations on the database.

    com.tutorialspoint.service.EmployeeService − A Service Class to implement the business opearations over repository functions.

    com.tutorialspoint.springbooth2.SprintBootH2Apppcation − A Spring Boot Apppcation class.

SprintBootH2Apppcation class is already present. We need to create the above packages and relevant classes and interface as shown below −

Entity - Entity.java

Following is the default code of Employee. It represents a Employee table with id, name, age and email columns.


package com.tutorialspoint.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
pubpc class Employee {
   @Id
   @Column
   private int id;
   @Column
   private String name;
   @Column
   private int age;
   @Column
   private String email;
   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc int getAge() {
      return age;
   }
   pubpc void setAge(int age) {
      this.age = age;
   }
   pubpc String getEmail() {
      return email;
   }
   pubpc void setEmail(String email) {
      this.email = email;
   }
}

Repository - EmployeeRepository.java

Following is the default code of Repository to implement CRUD operations on above entity, Employee.


package com.tutorialspoint.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.entity.Employee;
@Repository
pubpc interface EmployeeRepository extends CrudRepository<Employee, Integer>  {
}

Service - EmployeeService.java

Following is the default code of Service to implement operations over repository functions.


package com.tutorialspoint.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.repository.EmployeeRepository;
@Service
pubpc class EmployeeService {
   @Autowired
   EmployeeRepository repository;
   pubpc Employee getEmployeeById(int id) {
      return null;
   }
   pubpc List<Employee> getAllEmployees(){
      return null;
   }
   pubpc void saveOrUpdate(Employee employee) {
   }
   pubpc void deleteEmployeeById(int id) {
   }
}

Controller - EmployeeController.java

Following is the default code of Controller to implement REST APIs.


package com.tutorialspoint.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.service.EmployeeService;
@RestController
@RequestMapping(path = "/emp")
pubpc class EmployeeController {
   @Autowired
   EmployeeService employeeService;
   @GetMapping("/employees")
   pubpc List<Employee> getAllEmployees(){
      return null;
   }
   @GetMapping("/employee/{id}")
   pubpc Employee getEmployee(@PathVariable("id") int id) {
      return null;;
   }
   @DeleteMapping("/employee/{id}")
   pubpc void deleteEmployee(@PathVariable("id") int id) {
   }
   @PostMapping("/employee")
   pubpc void addEmployee(@RequestBody Employee employee) {
   }
   @PutMapping("/employee")
   pubpc void updateEmployee(@RequestBody Employee employee) {
   }	
}

Apppcation - SprintBootH2Apppcation.java

Following is the updated code of Apppcation to use above classes.


package com.tutorialspoint.sprintbooth2;
import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@ComponentScan({"com.tutorialspoint.controller","com.tutorialspoint.service"})
@EntityScan("com.tutorialspoint.entity")
@EnableJpaRepositories("com.tutorialspoint.repository")
@SpringBootApppcation
pubpc class SprintBootH2Apppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(SprintBootH2Apppcation.class, args);
   }
}

Run/Debug Configuration

Create following maven configuration in ecppse to run the springboot apppcation with goal spring-boot:run. This configuration will help to run the REST APIs and we can test them using POSTMAN.

Maven Configuration Advertisements