A Spring Boot application demonstrating multiple database configurations with PostgreSQL for Teachers and MySQL for Students.
- PostgreSQL: Manages Teacher data
- MySQL: Manages Student data
- Spring Data JPA: ORM for database operations
- REST API: Full CRUD operations for both entities
- Java 26+
- Maven 3.8+
- PostgreSQL 12+
- MySQL 8.0+
# Connect to PostgreSQL
psql -U postgres
# Run the setup script
\i setup-postgres.sqlOr manually:
CREATE DATABASE classroom_db;
-- Then run the SQL commands from setup-postgres.sql# Connect to MySQL
mysql -u root -p
# Run the setup script
source setup-mysql.sql;Or manually:
CREATE DATABASE classroom_student_db;
-- Then run the SQL commands from setup-mysql.sqlEdit src/main/resources/application.properties with your database credentials:
# PostgreSQL
spring.datasource.primary.url=jdbc:postgresql://localhost:5432/classroom_db
spring.datasource.primary.username=postgres
spring.datasource.primary.password=your_password
# MySQL
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/classroom_student_db
spring.datasource.secondary.username=root
spring.datasource.secondary.password=your_password# Build the project
mvn clean install
# Run the application
mvn spring-boot:runThe application will start on http://localhost:8080
Create Teacher
POST /api/teachers
Content-Type: application/json
{
"name": "Dr. John Smith",
"email": "john.smith@school.com",
"phone": "+1234567890",
"specialization": "Mathematics",
"experience": 10
}Get All Teachers
GET /api/teachersGet Teacher by ID
GET /api/teachers/{id}Update Teacher
PUT /api/teachers/{id}
Content-Type: application/json
{
"name": "Dr. John Smith",
"phone": "+1234567890",
"specialization": "Mathematics",
"experience": 11
}Delete Teacher
DELETE /api/teachers/{id}Create Student
POST /api/students
Content-Type: application/json
{
"name": "Alice Brown",
"email": "alice.brown@school.com",
"phone": "+1234567893",
"studentId": "STU001",
"className": "Class 10-A",
"gpa": 3.8
}Get All Students
GET /api/studentsGet Student by ID
GET /api/students/{id}Update Student
PUT /api/students/{id}
Content-Type: application/json
{
"name": "Alice Brown",
"phone": "+1234567893",
"className": "Class 10-A",
"gpa": 3.85
}Delete Student
DELETE /api/students/{id}src/main/java/com/nanjung/connect/db/example/
βββ config/
β βββ PostgresConfig.java # PostgreSQL configuration
β βββ MysqlConfig.java # MySQL configuration
βββ entity/
β βββ teacher/
β β βββ Teacher.java
β βββ student/
β βββ Student.java
βββ repository/
β βββ teacher/
β β βββ TeacherRepository.java
β βββ student/
β βββ StudentRepository.java
βββ service/
β βββ teacher/
β β βββ TeacherService.java
β βββ student/
β βββ StudentService.java
βββ controller/
β βββ teacher/
β β βββ TeacherController.java
β βββ student/
β βββ StudentController.java
βββ dto/
β βββ teacher/
β β βββ TeacherDTO.java
β βββ student/
β βββ StudentDTO.java
βββ ExampleConnectDbApplication.java
# Create teacher
curl -X POST http://localhost:8080/api/teachers \
-H "Content-Type: application/json" \
-d '{
"name": "New Teacher",
"email": "new.teacher@school.com",
"phone": "+1234567899",
"specialization": "Chemistry",
"experience": 5
}'
# Get all teachers
curl http://localhost:8080/api/teachers
# Get specific teacher
curl http://localhost:8080/api/teachers/1
# Update teacher
curl -X PUT http://localhost:8080/api/teachers/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Teacher",
"phone": "+1234567899",
"specialization": "Chemistry",
"experience": 6
}'
# Delete teacher
curl -X DELETE http://localhost:8080/api/teachers/1# Create student
curl -X POST http://localhost:8080/api/students \
-H "Content-Type: application/json" \
-d '{
"name": "New Student",
"email": "new.student@school.com",
"phone": "+1234567898",
"studentId": "STU999",
"className": "Class 10-C",
"gpa": 3.75
}'
# Get all students
curl http://localhost:8080/api/students
# Get specific student
curl http://localhost:8080/api/students/1
# Update student
curl -X PUT http://localhost:8080/api/students/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Student",
"phone": "+1234567898",
"className": "Class 10-C",
"gpa": 3.8
}'
# Delete student
curl -X DELETE http://localhost:8080/api/students/1- PostgresConfig.java: Configures PostgreSQL as primary datasource
- MysqlConfig.java: Configures MySQL as secondary datasource
- Separate EntityManagerFactory for each database
- Separate TransactionManager for each database
- JpaRepositories scan specific packages
| Column | Type | Constraints |
|---|---|---|
| id | BIGSERIAL | PRIMARY KEY |
| name | VARCHAR(100) | NOT NULL |
| VARCHAR(100) | UNIQUE, NOT NULL | |
| phone | VARCHAR(20) | NOT NULL |
| specialization | VARCHAR(100) | NOT NULL |
| experience | INTEGER | NOT NULL |
| created_at | TIMESTAMP | NOT NULL |
| updated_at | TIMESTAMP | NOT NULL |
| Column | Type | Constraints |
|---|---|---|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
| name | VARCHAR(100) | NOT NULL |
| VARCHAR(100) | UNIQUE, NOT NULL | |
| phone | VARCHAR(20) | NOT NULL |
| student_id | VARCHAR(50) | UNIQUE, NOT NULL |
| class_name | VARCHAR(50) | NOT NULL |
| gpa | DOUBLE | NOT NULL |
| created_at | TIMESTAMP | NOT NULL |
| updated_at | TIMESTAMP | NOT NULL |
The application includes error handling for:
- Duplicate email addresses
- Duplicate student IDs
- Resource not found errors
- Validation errors
- Spring Boot 4.1.0
- Spring Data JPA
- PostgreSQL Driver
- MySQL Connector
- Lombok
- Spring Web
β Multiple database support (PostgreSQL + MySQL) β Complete CRUD operations β Separate JPA configurations per database β Transaction management per database β RESTful API endpoints β Data validation and error handling β Automatic timestamp management
- Ensure PostgreSQL and MySQL are running
- Check database URLs and credentials in application.properties
- Run the setup SQL scripts
- Check database names match in properties
mvn clean install -U- Ensure Java 26+ is installed
- Run
mvn cleanbefore building
For issues or questions, please refer to:
- Spring Boot Documentation: https://spring.io/projects/spring-boot
- Spring Data JPA: https://spring.io/projects/spring-data-jpa
Version: 1.0.0
Last Updated: 2026-07-09
Author: