This project is dedicated to learning and experimenting with Spring Boot, Java Persistence API (JPA), MySQL integration, annotations, testing practices, and other important concepts related to full-stack development in Java. The project aims to build a simple book library management system where users can add, delete, and search books.
This project is an excellent resource for anyone looking to understand how to use Spring Boot in combination with MySQL for building backend applications.
- Book Management: Add, delete, and view books in the system.
- Author Search: Search books by author name.
- Database Integration: Utilizes MySQL database for persistent storage.
- Testing: Unit tests for controller and repository layers using Mockito and MockMvc for web-layer testing.
This project is designed to help me understand the following key concepts:
- Spring Boot: Building RESTful APIs and microservices with Spring Boot.
- Annotations: Using various Spring annotations like
@RestController,@Autowired,@RequestMapping,@GetMapping,@PostMapping, etc., to simplify the development process. - MySQL Integration: Connecting Spring Boot with MySQL and performing CRUD operations using JPA.
- Testing: Writing unit tests with JUnit, Mockito, and MockMvc for API endpoints.
- Repository Layer: Using Spring Data JPA for interacting with the database and handling entities like
Book.
- Java 17 or higher
- Maven for dependency management
- MySQL 5.7 or higher
- IDE: IntelliJ IDEA, Eclipse, or any other Java IDE of your choice
-
Clone this repository:
git clone https://github.com/yourusername/book-library.git
-
Create a MySQL database named
book_db:CREATE DATABASE book_db;
-
Update
application.propertiesorapplication-test.propertieswith your database credentials:spring.datasource.url=jdbc:mysql://localhost:3306/book_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=Password1 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # JPA / Hibernate spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
-
Run the project using Maven:
mvn spring-boot:run
The API exposes the following endpoints:
-
POST /api/books: Add a new book.
- Request Body: JSON object with
titleandauthorfields. - Response:
Book added successfullyorBook already exists.
- Request Body: JSON object with
-
GET /api/books: Get all books.
- Response: List of all books in the database.
-
GET /api/books/author: Get books by a specific author.
- Query Parameter:
author - Response: List of books by the specified author.
- Query Parameter:
-
DELETE /api/books/{id}: Delete a book by its ID.
- Response:
Book deleted successfullyorBook not found.
- Response:
Unit and integration tests are provided for the controller and repository layers.
- JUnit 5: For running unit tests.
- Mockito: For mocking services and repositories in unit tests.
- MockMvc: For testing web layer controllers without starting a full HTTP server.
To run the tests, use Maven:
mvn test