# dynamic-db **Repository Path**: DimonHo/dynamic-db ## Basic Information - **Project Name**: dynamic-db - **Description**: spingboot + mybatis-plus + druid 动态数据源 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-24 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 项目依赖于mybatis-plus 连接池使用alibaba的druid 可以添加任意个数据源,通过AOP的方式进行动态切换 # 快速开始 ### 动态切换数据源 1. `application.yml`数据源配置 ```yaml spring: aop: proxy-target-class: true auto: true datasource: source: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://localhost:3306/dynamic-source?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false username: ${dbUser:root} password: ${dbPass:wdkj@123#} db-type: mysql validation-query: "select 1" max-active: 16 min-idle: 4 target: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://localhost:3306/dynamic-target?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 db-type: mysql validation-query: "select 1" druid: filter: stat: enabled: true #是否激活sql监控 wall: enabled: true #是否激活sql防火墙 web-stat-filter: enabled: true stat-view-servlet: enabled: true url-pattern: "/druid/*" #浏览器访问localhost:8080/druid,进入监控界面 login-username: admin login-password: admin@123 ``` 2. 根据需求修改配置类`DbGlobal`以及配置`DataSourceAspect`和`MybatisPlusConfig`类 ### 自动生成代码 1.自动生成代码需要依赖 ```xml com.baomidou mybatis-plus-generator 3.2.0 ``` 2.在application.yml中打开开关 ```yaml mybatis-plus: auto-generator: true ## 自动生成代码的开关,默认为false ``` 以上准备好之后,just run!