multidatasources-spring-boot-starter
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||
一:下载multidatasources-spring-boot-starter,并安装到本地 二:springboot项目中的pom文件加入依赖,地址如下 <dependency> <groupId>com.yonyou.multidatasources</groupId> <artifactId> multidatasources-spring-boot-starter </artifactId> <version>0.9.1-SNAPSHOT</version> </dependency> 三:修改项目的配置文件application.yml,配置自己的数据源,其中datasource下一级约定为default、two、three、four、five,default为必配 multi: datasource: default: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/lin_infrastructure?useUnicode=true&characterEncoding=UTF-8 username: password: two: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db_slave1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true username: password: three: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db_slave2?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true username: password: 四:在service层的方法上使用注解来切换数据库,枚举中DataSourceKey的DB_DEFAULT, DB_TWO, DB_THREE, DB_FOUR, DB_FIVE分别对应default、two、three、four、five,default 示例: @TargetDataSource(dataSourceKey = DataSourceKey.DB_THREE)对应multi.datasource.three配置的数据源