Skip to content

luiscoco/Spring_Data_JDBC__Lesson1_Basics-Sample1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Spring Data JDBC Basics Sample

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.

Requirements

  • Java 17+
  • Maven 3.9+

Running the app

mvn spring-boot:run

During startup the app:

  1. Recreates the in-memory schema defined in schema.sql and seeds sample data from data.sql.
  2. Clears all persisted aggregates via OrderRepository.
  3. Creates a new Order for “Alice” with two LineItems and saves it.
  4. 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

Project structure

  • JdbcBasicsApplication – Spring Boot launcher and demo CommandLineRunner that 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.

H2 console

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)

Building an executable JAR

mvn clean package

The runnable artifact lands in target/spring-data-jdbc-basics-sample-0.0.1-SNAPSHOT.jar.

Next steps

  • Swap in a persistent database by updating the datasource and removing the schema.sql/data.sql scripts.
  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages