This project is a compact Spring Boot application that demonstrates how to build and persist aggregate roots with Spring Data JDBC. It creates a simple orders domain composed of Order and LineItem entities backed by an in‑memory H2 database and prints formatted summaries to the console so you can quickly verify persistence behaviour.
- Java 17+
- Maven 3.9+
mvn spring-boot:runDuring startup the app:
- Recreates the in-memory schema defined in
schema.sqland seeds sample data fromdata.sql. - Clears all persisted aggregates via
OrderRepository. - Creates a new
Orderfor “Alice” with twoLineItems and saves it. - Prints the saved order, all orders, and the filtered list for Alice in an easy-to-read layout.
You should see console sections that look like:
-------- Saved Order --------
Order #3 for Alice
* Spring in Action x1 @ $39.90 -> $39.90
* Spring Data JDBC Guide x2 @ $29.90 -> $59.80
Total: $99.70
JdbcBasicsApplication– Spring Boot launcher and demoCommandLineRunnerthat seeds and prints orders.domain/Order&domain/LineItem– Aggregate root and value objects with explicit column mappings for H2.domain/OrderRepository– Spring Data JDBC repository with derived and annotated queries.schema.sql/data.sql– Database schema and bootstrap data executed automatically on startup.application.properties– H2 datasource configuration and console toggle.
You can inspect the in-memory database through the H2 console once the app is running:
- URL:
http://localhost:8080/h2-console - JDBC URL:
jdbc:h2:mem:ordersdb - User:
sa - Password: (leave blank)
mvn clean packageThe runnable artifact lands in target/spring-data-jdbc-basics-sample-0.0.1-SNAPSHOT.jar.
- Swap in a persistent database by updating the datasource and removing the
schema.sql/data.sqlscripts. - Expand the domain with additional aggregates or relationships to explore more Spring Data JDBC features.
- Add automated tests that exercise the repository and demo runner logic.