DEV Community

Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

4

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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay