A simple and clean Library Management System built using Java, Spring Boot, and PostgreSQL. This API allows you to:
- Manage books
- Manage borrowers
- Borrow and return books
- Track lending records
- View borrowing history
Everything is built with simplicity so anyone can easily set up, understand, and use the API.
- Add new books
- Update book details
- Delete books
- Get a single book
- Get all books
- Search books by title, author, or ISBN
- Register borrowers
- Update borrower details
- Delete borrowers
- View single borrower
- List all borrowers
- Borrow a book
- Return a book
- View borrowing history
- View currently borrowed books
src/main/java └── com.pl.librarymanagement ├── controller → Handles API endpoints ├── service → Main business logic ├── dto → Data Transfer Objects ├── entity → JPA Entities (Books, Borrowers, Lending Records) └── repository → JPA Repositories for DB access
This structure keeps the project clean, modular, and easy to extend.
Your PostgreSQL database contains 3 tables:
| Column | Type | Description |
|---|---|---|
| id | bigserial PK | Auto-generated |
| title | varchar | Book title |
| author | varchar | Author name |
| isbn | varchar unique | Unique book identifier |
| total_copies | int | Total copies in library |
| available_copies | int | Currently available copies |
| Column | Type | Description |
|---|---|---|
| id | bigserial PK | |
| name | varchar | |
| varchar unique | ||
| member_id | varchar unique |
| Column | Type | Description |
|---|---|---|
| id | bigserial PK | |
| book_id | FK → books | |
| borrower_id | FK → borrowers | |
| borrowed_at | date | |
| due_date | date | |
| returned_at | date nullable |
Borrower (1) ----- (M) LendingRecord (M) ----- (1) Book
Meaning:
- A borrower can borrow many books
- A book can be borrowed many times
- Every borrowing action creates a lending record
This project includes a Postman collection for API documentation and testing.
Location: postman/library-management.postman_collection.json
To use:
- Open Postman
- Click Import
- Select the JSON file
- Run the requests
This section shows the JSON format for all major endpoints in the API.
{
"title": "Clean Code",
"author": "Robert Martin",
"isbn": "9780132350884",
"totalCopies": 10
}{
"id": 1,
"title": "Clean Code",
"author": "Robert Martin",
"isbn": "9780132350884",
"totalCopies": 10,
"availableCopies": 10
}{
"title": "Clean Code (Updated Edition)",
"author": "Robert Martin",
"totalCopies": 12,
"availableCopies": 8
}Response:
[
{
"id": 1,
"title": "Clean Code",
"author": "Robert Martin",
"isbn": "9780132350884",
"totalCopies": 10,
"availableCopies": 5
}
]{
"name": "John Doe",
"email": "john.doe@gmail.com",
"memberId": "MBR001"
}{
"id": 1,
"name": "John Doe",
"email": "john.doe@gmail.com",
"memberId": "MBR001"
}{
"name": "John Doe Updated",
"email": "updated.john@gmail.com"
}{
"bookId": 1,
"borrowerId": 3
}{
"id": 12,
"bookId": 1,
"bookTitle": "Clean Code",
"borrowerId": 3,
"borrowerName": "John Doe",
"borrowedAt": "2025-01-12",
"dueDate": "2025-01-26",
"returnedAt": null
}{
"lendingRecordId": 12
}{
"id": 12,
"bookId": 1,
"bookTitle": "Clean Code",
"borrowerId": 3,
"borrowerName": "John Doe",
"borrowedAt": "2025-01-12",
"dueDate": "2025-01-26",
"returnedAt": "2025-01-18"
}[
{
"id": 12,
"bookTitle": "Clean Code",
"borrowedAt": "2025-01-12",
"dueDate": "2025-01-26",
"returnedAt": "2025-01-18"
},
{
"id": 13,
"bookTitle": "Atomic Habits",
"borrowedAt": "2025-02-01",
"dueDate": "2025-02-15",
"returnedAt": null
}
][
{
"id": 13,
"bookId": 2,
"bookTitle": "Atomic Habits",
"borrowerId": 1,
"borrowerName": "John Doe",
"borrowedAt": "2025-02-01",
"dueDate": "2025-02-15",
"returnedAt": null
}
]| Method | Endpoint | Description |
|---|---|---|
| POST | /api/books | Add a book |
| PUT | /api/books/{id} | Update book info |
| DELETE | /api/books/{id} | Delete book |
| GET | /api/books/{id} | Get one book |
| GET | /api/books | Get all books |
| GET | /api/books/search/title | Search by title |
| GET | /api/books/search/author | Search by author |
| GET | /api/books/search/isbn | Search by ISBN |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/borrowers | Register borrower |
| PUT | /api/borrowers/{id} | Update borrower |
| DELETE | /api/borrowers/{id} | Delete borrower |
| GET | /api/borrowers/{id} | Get borrower |
| GET | /api/borrowers | List all borrowers |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/lending/borrow | Borrow a book |
| POST | /api/lending/return | Return a book |
| GET | /api/lending/history/{borrowerId} | Borrower history |
| GET | /api/lending/current | Currently borrowed books |
When you add a book, you provide title, author, ISBN, and total copies. The system automatically sets available copies equal to total copies.
Borrowers must have a name, email, and member ID. Emails and member IDs are unique.
The API:
-
Checks if the book exists
-
Checks if the borrower exists
-
Confirms the book has available copies
-
Reduces available copies by 1
-
Creates a lending record with:
- borrowed_at = today
- due_date = today + 14 days
- returned_at = null
The API:
- Retrieves the lending record
- Marks
returned_atas today - Increases book’s available copies by 1
Shows all books a borrower ever borrowed — both returned and currently borrowed.
Shows all lending records where returned_at is NULL.
- Java 17+
- Maven
- PostgreSQL installed
- pgAdmin (optional)
-
Create PostgreSQL database:
CREATE DATABASE librarydb; -
Update
application.properties:spring.datasource.url=jdbc:postgresql://localhost:5432/librarydb spring.datasource.username=postgres spring.datasource.password=YOUR_PASSWORD spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true -
Run the project:
mvn spring-boot:run
If you cannot run Docker, you can use Podman. It works exactly like Docker.
Example:
podman run -d -p 5432:5432 -e POSTGRES_PASSWORD=1234 postgres
If you want to improve:
- Add transaction logs
- Add email notifications
- Add admin roles
- Add fine/penalty for overdue books
PRs are welcome
This project is free to use and modify.