H2 is an open-source, in-memory database that is often used for testing and development purposes. It is lightweight, fast, and easy to use. Spring Boot is a popular Java framework for building web applications. It provides a number of features that make it easy to develop and deploy applications, including support for integrating with databases.
In this article, we will show you how to integrate the H2 database with Spring Boot. We will cover the following topics:
* Adding the H2 dependency to your project
* Configuring the H2 database
* Creating a Spring Data JPA repository
* Using the repository to perform CRUD operations
Adding the H2 dependency to your project
To add the H2 dependency to your project, add the following dependency to your pom.xml file:
“`xml
“`
Configuring the H2 database
Once you have added the H2 dependency to your project, you need to configure the database. You can do this by adding the following properties to your application.properties file:
“`properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
“`
The `spring.datasource.url` property specifies the URL of the database. The `spring.datasource.driverClassName` property specifies the driver class name for the database. The `spring.datasource.username` and `spring.datasource.password` properties specify the username and password for the database.
Creating a Spring Data JPA repository
Spring Data JPA is a framework that makes it easy to work with databases in Spring applications. To create a Spring Data JPA repository, you need to create an interface that extends the `JpaRepository` interface. The `JpaRepository` interface provides a number of methods for performing CRUD operations on a database.
For example, the following interface defines a Spring Data JPA repository for the `Book` entity:
“`java
public interface BookRepository extends JpaRepository
}
“`
Using the repository to perform CRUD operations
Once you have created a Spring Data JPA repository, you can use it to perform CRUD operations on the database. For example, the following code shows how to use the `BookRepository` to create, read, update, and delete books:
“`java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private BookRepository bookRepository;
@PostMapping(/books)
public Book createBook(@RequestBody Book book) {
return bookRepository.save(book);
}
@GetMapping(/books)
public List
return bookRepository.findAll();
}
@GetMapping(/books/{id})
public Book getBookById(@PathVariable Long id) {
return bookRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Book not found with id : + id));
}
@PutMapping(/books/{id})
public Book updateBook(@PathVariable Long id, @RequestBody Book bookDetails) {
Book book = bookRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Book not found with id : + id));
book.setTitle(bookDetails.getTitle());
book.setAuthor(bookDetails.getAuthor());
return bookRepository.save(book);
}
@DeleteMapping(/books/{id})
public ResponseEntity> deleteBook(@PathVariable Long id) {
bookRepository.deleteById(id);
return ResponseEntity.ok().build();
}
}
“`
Kind regards
R. Morris