本篇內(nèi)容介紹了“AbstractRoutingDataSource AOP如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、代縣網(wǎng)站維護(hù)、網(wǎng)站推廣。
## datasource master # spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.MySQL.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/master?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 ## datasource slave # spring.datasource-slave.type=com.alibaba.druid.pool.DruidDataSource spring.datasource-slave.driver-class-name=com.mysql.jdbc.Driver spring.datasource-slave.url=jdbc:mysql://localhost:3306/slave?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource-slave.username=root spring.datasource-slave.password=123456
public interface Datasources { String MASTER_DB = "masterDB"; String SLAVE_DB = "slaveDB"; }
@Configuration public class DataSourceConfig { //destroy-method="close"的作用是當(dāng)數(shù)據(jù)庫連接不使用的時(shí)候,就把該連接重新放到數(shù)據(jù)池中,方便下次使用調(diào)用. @Bean(destroyMethod = "close", name = Datasources.MASTER_DB) @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(destroyMethod = "close", name = Datasources.SLAVE_DB) @ConfigurationProperties(prefix = "spring.datasource-slave") public DataSource dataSourceSlave() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } }
@Configuration @MapperScan(basePackages = {"com.example.dao"}) public class MybatisConfig { @Autowired @Qualifier(Datasources.MASTER_DB) private DataSource masterDB; @Autowired @Qualifier(Datasources.SLAVE_DB) private DataSource slaveDB; /** * 動(dòng)態(tài)數(shù)據(jù)源 */ @Bean(name = "dynamicDataSource") public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); // 默認(rèn)數(shù)據(jù)源 dynamicDataSource.setDefaultTargetDataSource(masterDB); // 配置多數(shù)據(jù)源 Map<Object, Object> dsMap = new HashMap<>(); dsMap.put(Datasources.MASTER_DB, masterDB); dsMap.put(Datasources.SLAVE_DB, slaveDB); dynamicDataSource.setTargetDataSources(dsMap); return dynamicDataSource; } @Bean @ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // 配置數(shù)據(jù)源,此處配置為關(guān)鍵配置,如果沒有將 dynamicDataSource 作為數(shù)據(jù)源則不能實(shí)現(xiàn)切換 sqlSessionFactoryBean.setDataSource(dynamicDataSource()); return sqlSessionFactoryBean; } }
public class DataSourceContextHolder { /** * 默認(rèn)數(shù)據(jù)源 */ public static final String DEFAULT_DATASOURCE = Datasources.MASTER_DB; private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); // 設(shè)置數(shù)據(jù)源名 public static void setDB(String dbType) { System.out.println("切換到{}數(shù)據(jù)源:" + dbType); contextHolder.set(dbType); } // 獲取數(shù)據(jù)源名 public static String getDB() { return (contextHolder.get()); } // 清除數(shù)據(jù)源名 public static void clearDB() { contextHolder.remove(); } }
@Aspect @Component public class DynamicDataSourceAspect { @Before("@annotation(com.example.util.RoutingDataSource)") public void beforeSwitchDS(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); String dataSource = DataSourceContextHolder.DEFAULT_DATASOURCE; if (method.isAnnotationPresent(RoutingDataSource.class)) { RoutingDataSource routingDataSource = method.getDeclaredAnnotation(RoutingDataSource.class); dataSource = routingDataSource.value(); } DataSourceContextHolder.setDB(dataSource); } @After("@annotation(com.example.util.RoutingDataSource)") public void afterSwitchDS(JoinPoint point) { DataSourceContextHolder.clearDB(); } }
public class DynamicDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { System.out.println("數(shù)據(jù)源為{}:" + DataSourceContextHolder.getDB()); return DataSourceContextHolder.getDB(); } }
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class CutDataBaseApplication { public static void main(String[] args) { SpringApplication.run(CutDataBaseApplication.class, args); } }
/** * @author aYong * @version 1.0 * @date 2019/7/24 */ @RestController @RequestMapping("/route") public class SysUserController { @Autowired private SysUserService sysUserService; @GetMapping("/test1") public SysUser test1(long id) { return sysUserService.test1(id); } @GetMapping("/test2") public Integer test2(long id, String name) { return sysUserService.test2(id, name); } }
“AbstractRoutingDataSource AOP如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
名稱欄目:AbstractRoutingDataSourceAOP如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換
路徑分享:http://jinyejixie.com/article14/jjppge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站導(dǎo)航、電子商務(wù)、用戶體驗(yàn)、域名注冊(cè)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)