日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot+mybatis 多数据源切换

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot+mybatis 多数据源切换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于公司業務劃分了多個數據庫,開發一個項目會同時調用多個庫,經過學習我們采用了注解+aop的方式實現的

1.首先定義一個注解類

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface TargetDataSource {String value();//此處接收的是數據源的名稱 }

2.然后建一個配置類,這個在項目啟動時會加載數據源,一開始采用了HikariCP,查資料說是最快性能最好的,然后又發現了阿里的druid,這個功能比較全面,而且性能也還可以,最主要他還有監控功能,具體實現看如下代碼

?

package com.example.demo.datasource;import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import com.example.demo.datasource.DynamicDataSource; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.transaction.PlatformTransactionManager; import org.w3c.dom.NodeList; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node;import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.io.File; import com.alibaba.druid.support.http.StatViewServlet; /*** Author: wangchao* Version:* Date: 2017/9/11* Description:數據源配置* Modification History:* Date Author Version Description* --------------------------------------------------------------* Why & What is modified:*/@Configuration @EnableScheduling public class DataSourceConfig {/*@Autowiredprivate DBProperties properties;*/@Value("${datasource.filePath}")private String filePath;//數據源配置@Bean(name = "dataSource")public DataSource dataSource() {//按照目標數據源名稱和目標數據源對象的映射存放在Map中Map<Object, Object> targetDataSources = new HashMap<>();//查找xml數據連接字符串targetDataSources=getdataMap(filePath);//動態獲取DBProperties類申明的屬性/*Field[] fields=properties.getClass().getDeclaredFields();for(int i=0;i<fields.length;i++){targetDataSources.put(fields[i].getName(), getFieldValueByName(fields[i].getName(),properties));}*///采用是想AbstractRoutingDataSource的對象包裝多數據源DynamicDataSource dataSource = new DynamicDataSource();dataSource.setTargetDataSources(targetDataSources);//設置默認的數據源,當拿不到數據源時,使用此配置//dataSource.setDefaultTargetDataSource(properties.getUzaiTravel());return dataSource;}@Beanpublic PlatformTransactionManager txManager() {return new DataSourceTransactionManager(dataSource());}/***獲取數據源集合*/private Map<Object, Object> getdataMap(String fiePath){try {Map<Object, Object> targetDataSources = new HashMap<>();File xmlFile = new File(fiePath);DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = builderFactory.newDocumentBuilder();Document doc = builder.parse(xmlFile);doc.getDocumentElement().normalize();System.out.println("Root element: " + doc.getDocumentElement().getNodeName());NodeList nList = doc.getElementsByTagName("db");for(int i = 0 ; i<nList.getLength();i++) {Node node = nList.item(i);Element ele = (Element)node;/*HikariConfig config = new HikariConfig();config.setDriverClassName(ele.getElementsByTagName("driver-class").item(0).getTextContent());config.setJdbcUrl(ele.getElementsByTagName("jdbc-url").item(0).getTextContent());config.setUsername(ele.getElementsByTagName("username").item(0).getTextContent());config.setPassword(ele.getElementsByTagName("password").item(0).getTextContent());//config.addDataSourceProperty("password", ele.getElementsByTagName("password").item(0).getTextContent());HikariDataSource dataSource = new HikariDataSource(config);*/DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(ele.getElementsByTagName("driver-class").item(0).getTextContent());dataSource.setUsername(ele.getElementsByTagName("username").item(0).getTextContent());dataSource.setPassword(ele.getElementsByTagName("password").item(0).getTextContent());dataSource.setUrl(ele.getElementsByTagName("jdbc-url").item(0).getTextContent());dataSource.setInitialSize(5);dataSource.setMinIdle(1);dataSource.setMaxActive(10);// 啟用監控統計功能dataSource.setFilters("stat");//設置是否顯示sql語句targetDataSources.put(ele.getElementsByTagName("databasename").item(0).getTextContent(), dataSource);}return targetDataSources;}catch (Exception ex){return null;}}//訪問的ip@Value("${druid.IP}")private String IP;//登錄名@Value("${druid.druidLgoinName}")private String druidLgoinName;//密碼@Value("${druid.druidLgoinPassword}")private String druidLgoinPassword;@Beanpublic ServletRegistrationBean DruidStatViewServle() {//org.springframework.boot.context.embedded.ServletRegistrationBean提供類的進行注冊.ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");//添加初始化參數:initParams//白名單:servletRegistrationBean.addInitParameter("allow",IP);//IP黑名單 (存在共同時,deny優先于allow) : 如果滿足deny的話提示:Sorry, you are not permitted to view this page.// servletRegistrationBean.addInitParameter("deny", "192.168.1.73");//登錄查看信息的賬號密碼.servletRegistrationBean.addInitParameter("loginUsername",druidLgoinName);servletRegistrationBean.addInitParameter("loginPassword",druidLgoinPassword);//是否能夠重置數據.servletRegistrationBean.addInitParameter("resetEnable","false");return servletRegistrationBean;}/*** 注冊一個:filterRegistrationBean* @return*/@Beanpublic FilterRegistrationBean druidStatFilter2(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());//添加過濾規則.filterRegistrationBean.addUrlPatterns("/*");//添加不需要忽略的格式信息.filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}}

3.動態數據源,從之前已加載的數據源中選取,DynamicDataSource和DynamicDataSourceHolder配合使用

public class DynamicDataSource extends AbstractRoutingDataSource{//數據源路由,此方用于產生要選取的數據源邏輯名稱@Overrideprotected Object determineCurrentLookupKey() {//從共享線程中獲取數據源名稱return DynamicDataSourceHolder.getDataSource();} } public class DynamicDataSourceHolder {/*** 本地線程共享對象*/private static final ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<>();public static void putDataSource(String name) {THREAD_LOCAL.set(name);}public static String getDataSource() {return THREAD_LOCAL.get();}public static void removeDataSource() {THREAD_LOCAL.remove();} }

5.就是使用aop,在dao層切換數據源

@Component @Aspect public class DataSourceAspect {//切換放在mapper接口的方法上,所以這里要配置AOP切面的切入點@Pointcut("execution( * com.example.demo.dao.*.*(..))")public void dataSourcePointCut() {}@Before("dataSourcePointCut()")public void before(JoinPoint joinPoint) {Object target = joinPoint.getTarget();String method = joinPoint.getSignature().getName();Class<?>[] clazz = target.getClass().getInterfaces();Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();try {Method m = clazz[0].getMethod(method, parameterTypes);//如果方法上存在切換數據源的注解,則根據注解內容進行數據源切換if (m != null && m.isAnnotationPresent(TargetDataSource.class)) {TargetDataSource data = m.getAnnotation(TargetDataSource.class);String dataSourceName = data.value();DynamicDataSourceHolder.putDataSource(dataSourceName);} else {}} catch (Exception e) {}}//執行完切面后,將線程共享中的數據源名稱清空@After("dataSourcePointCut()")public void after(JoinPoint joinPoint){DynamicDataSourceHolder.removeDataSource();} }

  

 數據連接都配置在xml里面

?xml路徑在配置文件里面配置,這樣適用讀寫分離和多個不同的數據源,而且多個項目可以共用這一個配置

 

?最后引用注解,需要注意的是注解的數據庫名稱和xml里面databasename節點是一一對應的,可以隨便自定義,比如讀寫是一個數據庫名字,這時候就可以定義成pringtest_r表示讀庫

 

?

 至此多數據源就配置完成,至于阿里的druid下次再分享,代碼都貼出來,如果大家感覺還有哪些不足的地方,歡迎指正。

  

轉載于:https://www.cnblogs.com/ok123/p/7523106.html

總結

以上是生活随笔為你收集整理的spring boot+mybatis 多数据源切换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。