DEV Community

Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

Spring Boot + H2 Database Integration

Alt Text

Spring Boot [Rest API] - H2 Database

CRUD Operations using Spring Boot and H2 Database.

Installation

Download or Clone the repository from here:- Click Here

Import Maven based project in any of your Favourite IDE.

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Output

Open in Browser

Access all the endpoints using Swagger UI.

http://localhost:8080/swagger-ui.html
Enter fullscreen mode Exit fullscreen mode

Access H2 Database.

http://localhost:8080/h2-console
Enter fullscreen mode Exit fullscreen mode

Usage

PropertyService.java

addProperty() method Saving property to the in-memory database H2.

    public ResponseEntity<Object> addProperty(Property property) {
         Property savedProperty = propertyRepository.save(property);
         URI location =  ServletUriComponentsBuilder
         .fromCurrentRequest()
         .path("/{id}")
         .buildAndExpand(savedProperty.getId()).toUri();
         return ResponseEntity.created(location).build();
      }
Enter fullscreen mode Exit fullscreen mode

retriveAllProperties() Method to get all the properties from the DB.

public List<Property> retriveAllProperties() {
        return propertyRepository.findAll();
    }
Enter fullscreen mode Exit fullscreen mode

getPropertyByid() Method to find property by ID.

public Optional<Property> getPropertyById(Integer id) {
        return propertyRepository.findById(id);
    }
Enter fullscreen mode Exit fullscreen mode

deletePropertyByid() Method to delete property by ID.

public String deletePropertyById(Integer id) {
        propertyRepository.deleteById(id);
        return "Successfully Deleted property with ID:- " + id;
    }
Enter fullscreen mode Exit fullscreen mode

updateProperty() method to update existing property.

public ResponseEntity<Object> updateProperty(Property property) {
         Property savedProperty = propertyRepository.save(property);
         URI location =  ServletUriComponentsBuilder
         .fromCurrentRequest()
         .path("/{id}")
         .buildAndExpand(savedProperty.getId()).toUri();
         return ResponseEntity.created(location).build();
      }
Enter fullscreen mode Exit fullscreen mode

PropertyController.java

@GetMapping("/property")
    public List<Property> retriveAllProperties() {
        return propertyService.retriveAllProperties();
    }
Enter fullscreen mode Exit fullscreen mode
@PostMapping("/property")
    public ResponseEntity<Object> addProperty(@RequestBody Property property) {
         return propertyService.addProperty(property);
      }
Enter fullscreen mode Exit fullscreen mode
@GetMapping("/property/{id}")
    public Optional<Property> getPropertyById(@PathVariable Integer id) {
        return propertyService.getPropertyById(id);
    }
Enter fullscreen mode Exit fullscreen mode
@DeleteMapping("/property/{id}")
    public void deletePropertyById(@PathVariable Integer id) {
        propertyService.deletePropertyById(id);

    }
Enter fullscreen mode Exit fullscreen mode
@PatchMapping("/property")
    public ResponseEntity<Object> updateProperty(@RequestBody Property property) {
         return propertyService.updateProperty(property);
      }
Enter fullscreen mode Exit fullscreen mode

Configuration

application.properties

spring.jpa.show-sql = true

# Enabling H2 Console
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true

# Enable Web Access
spring.h2.console.settings.web-allow-others=true
Enter fullscreen mode Exit fullscreen mode

Credits

in28Minutes

Top comments (0)