This is a Spring Boot application that connects to a MySQL database using JPA. The project is containerized using Docker and can be run with Docker Compose. It includes REST APIs for managing users
and orders
.
- Docker and Docker Compose installed on your system.
- Java 17 installed if running locally.
- Maven installed for building the project.
02-my-java-db-app/
├── all_restart.sh # Script to rebuild and restart the full stack
├── app_restart.sh # Script to rebuild and restart only the application
├── docker-compose.all.yaml # Docker Compose file for the full stack
├── docker-compose.app.yaml # Docker Compose file for the application
├── docker-compose.db.yaml # Docker Compose file for the database
├── Dockerfile.app # Dockerfile for building the application container
├── Dockerfile.db # Dockerfile for building the database container
├── pom.xml # Maven configuration file
├── src/ # Source code for the application
│ ├── main/
│ │ ├── java/ # Java source files
│ │ └── resources/ # Application properties, schema, and data files
│ └── test/ # Unit tests
├── dbdata/ # Persistent database storage
└── docs/ # Documentation files
To start the MySQL database container:
docker-compose -f docker-compose.db.yaml up -d
To build and start the application container:
docker-compose -f docker-compose.app.yaml build
docker-compose -f docker-compose.app.yaml up -d
To stop the application and database containers:
docker-compose -f docker-compose.app.yaml down
docker-compose -f docker-compose.db.yaml down
To start or stop the full stack (application and database):
docker-compose -f docker-compose.all.yaml up -d
docker-compose -f docker-compose.all.yaml down
To rebuild and restart the full stack:
./all_restart.sh
To rebuild and restart only the application:
./app_restart.sh
The application uses the following environment variables for database configuration:
SPRING_DATASOURCE_URL
: JDBC URL for the database (default:jdbc:mysql://db:3306/mydatabase
)SPRING_DATASOURCE_USERNAME
: Database username (default:myuser
)SPRING_DATASOURCE_PASSWORD
: Database password (default:mypassword
)
These variables are defined in the docker-compose.app.yaml
file.
The application exposes the following REST API endpoints:
curl http://localhost:8080/api/healthcheck
- Get all users:
curl http://localhost:8080/api/users
- Create a new user:
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","email":"john@example.com"}' http://localhost:8080/api/users
- Get all orders:
curl http://localhost:8080/api/orders
- Create a new order:
curl -X POST -H "Content-Type: application/json" -d '{"userId":1,"productName":"Tablet","amount":500.00}' http://localhost:8080/api/orders
The database schema and initial data are defined in the following files:
src/main/resources/schema.sql
: Defines theusers
andorders
tables.src/main/resources/data.sql
: Inserts initial data into theusers
andorders
tables.
To manually inspect the database, connect to the MySQL container:
docker exec -it my-java-db-app-db-1 mysql -u myuser -pmypassword mydatabase
Run the following SQL commands:
SHOW TABLES;
SELECT * FROM users;
SELECT * FROM orders;
To run the unit tests:
mvn test
- If the application fails to start, check the logs:
docker logs <container_id>
- Ensure the database container is running and accessible.
- If the database schema or data is not initialized, ensure
schema.sql
anddata.sql
are in thesrc/main/resources
directory and that theapplication.properties
file has the correct configuration:spring.datasource.initialization-mode=always spring.jpa.defer-datasource-initialization=true
This project is licensed under the MIT License.