spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源_第1頁
spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源_第2頁
spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源_第3頁
spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源_第4頁
spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、spring-boot+dynamic-datasource實(shí)現(xiàn)真正的動(dòng)態(tài)數(shù)據(jù)源一、前言 現(xiàn)在有這樣一個(gè)連接表。數(shù)據(jù)庫連接信息是從連接表中獲取,想對連接表中的所有數(shù)據(jù)源進(jìn)行維護(hù)只有用動(dòng)態(tài)數(shù)據(jù)庫。在網(wǎng)上找了很多動(dòng)態(tài)數(shù)據(jù)庫教程。都是用Ds注解選擇需要的數(shù)據(jù)源,意思是在編寫代碼時(shí)就要確定數(shù)據(jù)源,并不能實(shí)現(xiàn)需求。想要對對應(yīng)的連接進(jìn)行管理,只有通過連接id能創(chuàng)建一個(gè)數(shù)據(jù)源,并使其生效,下面進(jìn)入正題。二、準(zhǔn)備1.兩個(gè)不同的數(shù)據(jù)源以及連接表2.主要依賴 com.baomidou mybatis-plus-boot-starter mysql mysql-connector-java 8.0.25 com.b

2、aomidou dynamic-datasource-spring-boot-starter 3.4.1 com.alibaba druid-spring-boot-starter 1.2.6 org.springframework.boot spring-boot-starter-aop 3.實(shí)體類Data TableName(dm_connect) ApiModel(value = Connect對象, description = ) public class Connect extends Model ApiModelProperty(value = 主鍵) TableId(value

3、= id, type = IdType.AUTO) private Integer id; ApiModelProperty(value = ip地址) TableField private String ipAddress; ApiModelProperty(value = 端口) TableField private Integer port; ApiModelProperty(value = 賬號(hào)) TableField private String username; ApiModelProperty(value = 密碼) TableField private String pass

4、word; TableName(information_schema.SCHEMATA) Data public class Schemata TableField(CATALOG_NAME) private String catalogName; TableField(SCHEMA_NAME) private String schemaName; TableField(DEFAULT_CHARACTER_SET_NAME) private String defaultCharacterSetName; TableField(DEFAULT_COLLATION_NAME) private St

5、ring defaultCollationName; TableField(SQL_PATH) private String sqlPath; TableField(DEFAULT_ENCRYPTION) private String defaultEncryption; 三、出發(fā)1.默認(rèn)數(shù)據(jù)源配置 默認(rèn)數(shù)據(jù)源主要功能是保存連接信息ymlspring: datasource: url: jdbc:mysql:/*.*.*.227:3306/mysql_manager?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroD

6、ateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true username: root password: zxc4256101 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 指定數(shù)據(jù)源類型MybatisPlusConfigMapperScan(自己定義mapper掃描邏輯) Configuration EnableTransactionManagement public c

7、lass MybatisPlusConfig Value($spring.datasource.url) private String url; Value($spring.datasource.username) private String username; Value($spring.datasource.password) private String password; Value($spring.datasource.driver-class-name) private String driverClassName; /* * * return 數(shù)據(jù)源配置 * throws SQ

8、LException */ Bean Primary ConfigurationProperties(prefix = spring.datasource.master) public DataSource dataSourceMaster() throws SQLException DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriver(new DruidDriver().createDriver(driverClassName); dataSource.

9、setUsername(username); dataSource.setPassword(password); dataSource.setUseGlobalDataSourceStat(true); dataSource.setPoolPreparedStatements(true); dataSource.setValidationQuery(select 1); dataSource.setTimeBetweenEvictionRunsMillis(6000); return dataSource; /* * 將默認(rèn)數(shù)據(jù)源添加進(jìn)動(dòng)態(tài)數(shù)據(jù)源 * param dataSourceMaste

10、r * return */ Bean(dynamicDataSource) public DataSource dynamicDataSource(DataSource dataSourceMaster) DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource(); dynamicRoutingDataSource.addDataSource(master, dataSourceMaster); return dynamicRoutingDataSource; Bean Primary C

11、onfigurationProperties(prefix = mybatis) public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(Qualifier(dynamicDataSource) DataSource dataSource, Qualifier(MybatisPlusInterceptor) MybatisPlusInterceptor mybatisPlusInterceptor) MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlS

12、essionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); /分頁插件 Interceptor plugins = new Interceptor1; plugins0 = mybatisPlusInterceptor; sqlSessionFactoryBean.setPlugins(plugins); return sqlSessionFactoryBean; /* * myBatis-plus 分頁查詢配置 */ Bean(name=MybatisPlusInterceptor) public Mybatis

13、PlusInterceptor MpPaginationInterceptor() MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL); return interceptor; /在方法上使用 Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Documented public inter

14、face Dynamic /* * 數(shù)據(jù)庫名稱 */ String database() default ; Aspect Component Slf4j RequiredArgsConstructor public class DynamicAspect public static final String ID = id; public static final String TABLE_SCHEMA=tableSchema; public static final String DS_NAME = dynamic; private final IConnectService connec

15、tService; private final DynamicRoutingDataSource dynamicRoutingDataSource; Pointcut(annotation(包名.annotation.Dynamic) public void pointcut() /* * 進(jìn)入方法前通過連接id定義一個(gè)數(shù)據(jù)源并設(shè)為默認(rèn) * param joinPoint */ Before(pointcut() public void before(JoinPoint joinPoint) MethodSignature ms = (MethodSignature) joinPoint.ge

16、tSignature(); Method method = ms.getMethod(); Dynamic dynamic = method.getAnnotation(Dynamic.class); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes & null != attributes.getRequest().getParameter(ID) /獲取請求頭參數(shù) Intege

17、r id = Integer.parseInt(attributes.getRequest().getParameter(ID); /數(shù)據(jù)庫名稱:information_schema請求注解 String database=information_schema; /從請求中獲取數(shù)據(jù)庫名 if(StringUtils.isNotBlank(attributes.getRequest().getParameter(TABLE_SCHEMA) database=attributes.getRequest().getParameter(TABLE_SCHEMA); /從注解上獲取數(shù)據(jù)庫名稱 if(St

18、ringUtils.isNotBlank(dynamic.database() database=dynamic.database(); /獲取連接參數(shù) Connect connect = connectService.getById(id); if (null = connect) throw new RuntimeException(連接不存在!); /組裝datasource DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(jdbc:mysql:/ + connect.getIpAddress()

19、 + : + connect.getPort() + / + database); dataSource.setUsername(connect.getUsername(); dataSource.setPassword(connect.getPassword(); /添加datasource到動(dòng)態(tài)數(shù)據(jù)源并設(shè)為primary dynamicRoutingDataSource.addDataSource(dynamic, dataSource); dynamicRoutingDataSource.setPrimary(dynamic); Around(pointcut() public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable try return joinPceed(); catch (Throwable throwable) /方法出錯(cuò)時(shí)重置數(shù)據(jù)源 throwable.printStackTrace(); dynamicRoutingDataSource.setPrimary(master); dynamicRoutingDataSource.removeDataSource(dynamic); return null; /* * 在切入點(diǎn)return內(nèi)容之后切入內(nèi)容(可以用來對處理返

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論