# study-project **Repository Path**: L1nYuan/study-project ## Basic Information - **Project Name**: study-project - **Description**: 学习使用springboot、shardingjdbc、redis、layui - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-12-03 - **Last Updated**: 2022-12-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Study-demo #### 介绍 Springboot+Mybatis-plus 实现动态数据源切换 实现多数据源,动态数据源切换,读写分离等 #### 软件架构 软件架构说明 Springboot+Mybatis-plus #### 安装教程 1. 创建数据库和表 2. 配置jdbc多数据源 3. mp逆向工程生成代码 #### 使用说明 1. 在本地数据库上创建三个数据库test1,test2,test3,每个库建一张表sys_user,这里是MySQL ![img.png](img.png) 2. 拉取项目后,在application.properties中配置连接数据源的内容 ```git remote add origin https://gitee.com/L1nYuan/study-demo.git other.datasource.names=test2,test3 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test1?useSSL=false&characterEncoding=UTF-8&useUnicode=true&serverTimezone=UTC&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.test2.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.test2.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2?useSSL=false&characterEncoding=UTF-8&useUnicode=true&serverTimezone=UTC&allowPublicKeyRetrieval=true spring.datasource.test2.username=root spring.datasource.test2.password=123456 spring.datasource.test3.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.test3.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.test3.url=jdbc:mysql://localhost:3306/test3?useSSL=false&characterEncoding=UTF-8&useUnicode=true&serverTimezone=UTC&allowPublicKeyRetrieval=true spring.datasource.test3.username=root spring.datasource.test3.password=123456 ``` 3. 启动CodeGenerator的main方法生成代码 4. 使用@datasource注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解 ``` @Mapper public interface SysUserMapper extends BaseMapper { @DataSource // 使用默认数据源 List findAllListByTest1(); @DataSource("test2") // 使用test2数据源 List findAllListByTest2(); @DataSource("test3") // 使用test3数据源 List findAllListByTest3(); } ```