# springboot-muti-datasource-demo **Repository Path**: xieliang85a/springboot-muti-datasource-demo ## Basic Information - **Project Name**: springboot-muti-datasource-demo - **Description**: 基于springboot实现多数据源的示例项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-25 - **Last Updated**: 2025-04-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AIS Receive Application A Spring Boot application with MyBatis Plus for handling ship information. ## Technologies Used * Java 8 * Spring Boot 2.3.x * MyBatis Plus 3.4.x * Druid (connection pool) * MySQL / DaMeng Database * Maven * Tomcat 8.x ## Project Structure ``` springboot-muti-datasource/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── aisreceiveapp/ │ │ │ ├── controller/ # REST API controllers │ │ │ ├── entity/ # Domain model objects │ │ │ ├── mapper/ # MyBatis mappers │ │ │ ├── service/ # Business logic │ │ │ └── AisReceiveApplication.java # Main app class │ │ └── resources/ │ │ ├── mapper/ # MyBatis XML mapper files │ │ └── application.yml # Application configuration ├── lib/ # External libraries (e.g., DaMeng JDBC driver) ├── sql/ # Database scripts └── pom.xml # Maven dependencies and build config ``` ## Multiple Datasources The application supports both MySQL and DaMeng databases with dynamic datasource switching capabilities: ### Configuration Datasources are configured in `src/main/resources/application.yml` with MySQL set as the primary datasource: ```yaml spring: datasource: dynamic: primary: mysql datasource: mysql: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ais_db username: root password: root dameng: driver-class-name: dm.jdbc.driver.DmDriver url: jdbc:dm://localhost:5236/ais_db username: SYSDBA password: SYSDBA ``` ### Usage The application uses the `@DS` annotation to specify which datasource to use: 1. **Class-level annotation**: Sets the default datasource for all methods in the class ```java @Service @DS("mysql") public class ShipServiceImpl implements ShipService { // All methods use MySQL by default } ``` 2. **Method-level annotation**: Overrides the class-level datasource for specific methods ```java @Service @DS("mysql") public class ShipServiceImpl implements ShipService { // This method will use DaMeng instead of MySQL @DS("dameng") public List getShipsFromDaMeng() { return list(); } } ``` 3. **Mapper-level annotation**: Sets the default datasource for all mapper methods ```java @Repository @DS("dameng") public interface ShipPositionMapper extends BaseMapper { // All methods use DaMeng by default } ``` 4. **Controller-level annotation**: Can be used to set datasource for specific API endpoints ```java @PostMapping("/mysql") @DS("mysql") public ResponseEntity addPositionToMysql(@RequestBody ShipPosition position) { // Operations here will use MySQL } ``` ## Database Configuration The database connection settings can be configured in `src/main/resources/application.yml`. ## Building the Project ```bash mvn clean package ``` This will generate a WAR file in the `target` directory. ## Deployment The WAR file can be deployed to Tomcat 8.x by placing it in the `webapps` directory of your Tomcat installation. ## Database Setup 1. Create the database and tables using the SQL scripts in the `sql` directory. 2. For MySQL, execute the `schema.sql` file directly. 3. For DaMeng database, use the commented section at the bottom of the `schema.sql` file. ## API Endpoints ### Ship Resource (MySQL) * `GET /ais-receive/api/ships` - Get all ships * `GET /ais-receive/api/ships/{id}` - Get ship by ID * `GET /ais-receive/api/ships/mmsi/{mmsi}` - Get ship by MMSI * `GET /ais-receive/api/ships/type/{shipType}` - Get ships by type * `POST /ais-receive/api/ships` - Add a new ship * `PUT /ais-receive/api/ships/{id}` - Update ship information * `DELETE /ais-receive/api/ships/{id}` - Delete a ship ### Ship Position Resource (DaMeng/MySQL) * `GET /ais-receive/api/positions/dameng/mmsi/{mmsi}` - Get positions by MMSI from DaMeng * `GET /ais-receive/api/positions/mysql/mmsi/{mmsi}` - Get positions by MMSI from MySQL * `GET /ais-receive/api/positions/dameng/timerange` - Get positions by time range from DaMeng * `POST /ais-receive/api/positions/dameng` - Add a position to DaMeng * `POST /ais-receive/api/positions/mysql` - Add a position to MySQL