# solitarysp/example-multiple-database-spring-boot architecture

> Architecture diagram of the GitHub repository solitarysp/example-multiple-database-spring-boot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/solitarysp/example-multiple-database-spring-boot
- Repository: https://github.com/solitarysp/example-multiple-database-spring-boot

## Overview

##### Project Type and Purpose
- This is a Spring Boot application that serves as a demo for configuring and using multiple databases—in this case, H2 databases.
- The project demonstrates how to manage more than one database configuration (e.g., for “User” and “Address”) and expose APIs (example: UserController) to interact with these databases.

##### Key Components
a. Application Bootstrap:
- MainApp.java: The entry point for the Spring Boot application.
b. API/Controller Layer:
- UserController.java: Handles HTTP requests and routes them to the backend logic.
c. Database Configuration and Connectivity:
- AddressDatabaseConfig.java and UserDatabaseConfig.java under the config/database package: These classes set up connections to the different H2 databases.
- application.properties and bootstrap.properties: Files defining configuration settings (database URLs, credentials, etc.).
d. Data Access Layer:
- Repository interfaces (AddressEntityRepo.java and UserRepo.java) for data CRUD operations.
e. Domain/Entity Layer:
- User.java and AddressEntity.java: Represent domain models used in the database.
f. Request/Response Handling:
- PostUserRQ.java for requests, along with response objects (ResponseBase, UsersRP, UsersDtoRp) outlining how data is structured in communication.
g. Additional Configs and Annotations:
- ConfigSwaggerConfig.java: Likely used for API documentation.
- ApiResponsesBase.java: A reusable annotation for API responses.

##### Relationships and Data Flow
- A client request first hits the UserController, which then processes the request by invoking methods on a relevant service or directly on the corresponding repository.
- The repositories, using Spring Data JPA, interact with the underlying H2 databases as per configurations defined in UserDatabaseConfig and AddressDatabaseConfig.
- The response is prepared using dedicated response objects and sent back to the client.
- External configuration files (data.sql, application.properties) initialize database schemas and set configuration values.

##### Architectural Patterns and Technologies
- Follow Spring Boot’s layered architecture: Controller → Repository → Database.
- Separation of concerns (each package handling its specific role).
- Multiple database configuration is a notable pattern here—a demonstration of handling diverse data sources.
- Integration of Swagger (via ConfigSwaggerConfig) for API documentation.
- H2 is used as an in-memory/demo database for simplicity.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Spring Boot Application
    A["MainApp.java"]:::main

    %% Controller Layer
    subgraph "Controller Layer"
        B["UserController.java"]:::controller
    end

    %% Request/Response Layer
    subgraph "Request/Response Layer"
        C["PostUserRQ.java"]:::reqres
        D["ResponseBase.java"]:::reqres
        E["UsersRP.java"]:::reqres
        F["UsersDtoRp.java"]:::reqres
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        G["UserRepo.java"]:::repo
        H["AddressEntityRepo.java"]:::repo
    end

    %% Database Config Layer
    subgraph "Database Config Layer"
        I["UserDatabaseConfig.java"]:::config
        J["AddressDatabaseConfig.java"]:::config
    end

    %% Domain/Entity Layer
    subgraph "Domain/Entity Layer"
        K["User.java"]:::entity
        L["AddressEntity.java"]:::entity
    end

    %% API Documentation/Annotations
    subgraph "API Documentation/Annotations"
        M["ConfigSwaggerConfig.java"]:::doc
        N["ApiResponsesBase.java"]:::doc
    end

    %% External Configuration Files
    subgraph "External Configuration Files"
        O["application.properties"]:::external
        P["bootstrap.properties"]:::external
        Q["data.sql"]:::external
    end

    %% Relationships
    A -->|"initializes"| B
    B -->|"handlesRequest"| C
    B -->|"invokes"| G
    B -->|"invokes"| H
    G -->|"uses"| I
    H -->|"uses"| J
    I <-->|"configFrom"| O
    I <-->|"configFrom"| P
    I <-->|"configFrom"| Q
    J <-->|"configFrom"| O
    J <-->|"configFrom"| P
    J <-->|"configFrom"| Q
    G -->|"manages"| K
    H -->|"manages"| L
    M -->|"docsFor"| B
    N -->|"annotates"| B

    %% Styles
    classDef main fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef controller fill:#F9E79F,stroke:#D68910,stroke-width:2px;
    classDef reqres fill:#FDEDEC,stroke:#C0392B,stroke-width:2px;
    classDef repo fill:#FADBD8,stroke:#E74C3C,stroke-width:2px;
    classDef config fill:#D6EAF8,stroke:#3498DB,stroke-width:2px;
    classDef entity fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef external fill:#ECF0F1,stroke:#95A5A6,stroke-width:2px;
    classDef doc fill:#FCF3CF,stroke:#F1C40F,stroke-width:2px;

    %% Click Events
    click A "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/MainApp.java"
    click B "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/controller/UserController.java"
    click I "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/config/database/UserDatabaseConfig.java"
    click J "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/config/database/AddressDatabaseConfig.java"
    click O "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/resources/application.properties"
    click P "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/resources/bootstrap.properties"
    click Q "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/resources/data.sql"
    click G "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/repo/user/UserRepo.java"
    click H "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/repo/adress/AddressEntityRepo.java"
    click K "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/entity/user/User.java"
    click L "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/entity/adress/AddressEntity.java"
    click C "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/request/PostUserRQ.java"
    click D "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/reponse/ResponseBase.java"
    click E "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/reponse/UsersRP.java"
    click F "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/reponse/dto/UsersDtoRp.java"
    click M "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/config/ConfigSwaggerConfig.java"
    click N "https://github.com/solitarysp/example-multiple-database-spring-boot/blob/master/src/main/java/com/lethanh98/annotation/ApiResponsesBase.java"
```
