欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot+mybatis 多數(shù)據(jù)源切換(實例講解)

 更新時間:2017年09月15日 08:22:26   作者:易興  
下面小編就為大家?guī)硪黄猻pring boot+mybatis 多數(shù)據(jù)源切換(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

由于公司業(yè)務(wù)劃分了多個數(shù)據(jù)庫,開發(fā)一個項目會同事調(diào)用多個庫,經(jīng)過學(xué)習(xí)我們采用了注解+aop的方式實現(xiàn)的

1.首先定義一個注解類

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

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

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:數(shù)據(jù)源配置
 * Modification History:
 * Date    Author    Version   Description
 * --------------------------------------------------------------
 * Why & What is modified:
 */
 
@Configuration
@EnableScheduling
public class DataSourceConfig {
 
 /*@Autowired
 private DBProperties properties;*/
 @Value("${datasource.filePath}")
 private String filePath;//數(shù)據(jù)源配置
 
 @Bean(name = "dataSource")
 public DataSource dataSource() {
  //按照目標(biāo)數(shù)據(jù)源名稱和目標(biāo)數(shù)據(jù)源對象的映射存放在Map中
  Map<Object, Object> targetDataSources = new HashMap<>();
  //查找xml數(shù)據(jù)連接字符串
  targetDataSources=getdataMap(filePath);
  //動態(tài)獲取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的對象包裝多數(shù)據(jù)源
  DynamicDataSource dataSource = new DynamicDataSource();
  dataSource.setTargetDataSources(targetDataSources);
  //設(shè)置默認(rèn)的數(shù)據(jù)源,當(dāng)拿不到數(shù)據(jù)源時,使用此配置
  //dataSource.setDefaultTargetDataSource(properties.getUzaiTravel());
  return dataSource;
 }
 
 @Bean
 public PlatformTransactionManager txManager() {
  return new DataSourceTransactionManager(dataSource());
 }
 
 /**
 *獲取數(shù)據(jù)源集合
 */
 
 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);// 啟用監(jiān)控統(tǒng)計功能
    dataSource.setFilters("stat");//設(shè)置是否顯示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;
 
 @Bean
 public ServletRegistrationBean DruidStatViewServle() {
  //org.springframework.boot.context.embedded.ServletRegistrationBean提供類的進(jìn)行注冊.
  ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  //添加初始化參數(shù):initParams
 
  //白名單:
  servletRegistrationBean.addInitParameter("allow",IP);
  //IP黑名單 (存在共同時,deny優(yōu)先于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);
  //是否能夠重置數(shù)據(jù).
  servletRegistrationBean.addInitParameter("resetEnable","false");
  return servletRegistrationBean;
 }
 
 /**
 
  * 注冊一個:filterRegistrationBean
 
  * @return
 
 */
 @Bean
 public FilterRegistrationBean druidStatFilter2(){
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
  //添加過濾規(guī)則.
  filterRegistrationBean.addUrlPatterns("/*");
  //添加不需要忽略的格式信息.
  filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  return filterRegistrationBean;
 }
 
}

3.動態(tài)數(shù)據(jù)源,從之前已加載的數(shù)據(jù)源中選取,DynamicDataSource和DynamicDataSourceHolder配合使用

public class DynamicDataSource extends AbstractRoutingDataSource{
 //數(shù)據(jù)源路由,此方用于產(chǎn)生要選取的數(shù)據(jù)源邏輯名稱
 @Override
 protected Object determineCurrentLookupKey() {
  //從共享線程中獲取數(shù)據(jù)源名稱
  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();
 }
}

4.就是使用aop,在dao層切換數(shù)據(jù)源

@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);
   //如果方法上存在切換數(shù)據(jù)源的注解,則根據(jù)注解內(nèi)容進(jìn)行數(shù)據(jù)源切換
   if (m != null && m.isAnnotationPresent(TargetDataSource.class)) {
    TargetDataSource data = m.getAnnotation(TargetDataSource.class);
    String dataSourceName = data.value();
    DynamicDataSourceHolder.putDataSource(dataSourceName);
 
   } else {
 
   }
  } catch (Exception e) {
 
  }
 }
 
 //執(zhí)行完切面后,將線程共享中的數(shù)據(jù)源名稱清空
 @After("dataSourcePointCut()")
 public void after(JoinPoint joinPoint){
  DynamicDataSourceHolder.removeDataSource();
 }
}

數(shù)據(jù)連接都配置在xml里面

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

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

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

以上這篇spring boot+mybatis 多數(shù)據(jù)源切換(實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 多線程的同步代碼塊詳解

    Java 多線程的同步代碼塊詳解

    這篇文章主要介紹了Java 多線程的同步代碼塊,使用synchronized關(guān)鍵字創(chuàng)建線程同步方法是實現(xiàn)線程同步的關(guān)鍵,需要的朋友可以參考下
    2021-10-10
  • 詳談java 堆區(qū)、方法區(qū)和棧區(qū)

    詳談java 堆區(qū)、方法區(qū)和棧區(qū)

    下面小編就為大家?guī)硪黄斦刯ava 堆區(qū)、方法區(qū)和棧區(qū)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • IntelliJ IDEA Tomcat控制臺中文亂碼問題的四種解決方案

    IntelliJ IDEA Tomcat控制臺中文亂碼問題的四種解決方案

    這篇文章主要給大家分享了4種方法完美解決IntelliJ IDEA Tomcat控制臺中文亂碼問題,文中有詳細(xì)的圖文介紹,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-08-08
  • 使用Spring Boot的LoggersEndpoint管理日志級別

    使用Spring Boot的LoggersEndpoint管理日志級別

    這篇文章主要為大家介紹了使用Spring Boot的LoggersEndpoint管理日志級別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • SpringBoot邏輯異常統(tǒng)一處理方法

    SpringBoot邏輯異常統(tǒng)一處理方法

    這篇文章主要介紹了SpringBoot邏輯異常統(tǒng)一處理方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • 教你利用SpringBoot寫一個屬于自己的Starter

    教你利用SpringBoot寫一個屬于自己的Starter

    如果我們將可獨立于業(yè)務(wù)代碼之外的功配置模塊封裝成一個個starter,復(fù)用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配,簡直不要太爽,這篇文章主要給大家介紹了關(guān)于如何利用SpringBoot寫一個屬于自己的Starter,需要的朋友可以參考下
    2022-03-03
  • SpringBoot yml配置文件讀取方法詳解

    SpringBoot yml配置文件讀取方法詳解

    這篇文章主要介紹了SpringBoot yml配置文件讀取方法,項目開發(fā)中難免要讀取配置文件,本文結(jié)合開發(fā)經(jīng)驗介紹幾種使用過的讀取配置文件的方法
    2022-10-10
  • SpringBoot項目解決跨域的四種方案分享

    SpringBoot項目解決跨域的四種方案分享

    在用SpringBoot開發(fā)后端服務(wù)時,我們一般是提供接口給前端使用,但前端通過瀏覽器調(diào)我們接口時,瀏覽器會有個同源策略的限制,即協(xié)議,域名,端口任一不一樣時都會導(dǎo)致跨域,這篇文章主要介紹跨域的幾種常用解決方案,希望對大家有所幫助
    2023-05-05
  • Java中的三元運算(三目運算)以后用得到!

    Java中的三元運算(三目運算)以后用得到!

    Java提供了一個三元運算符,可以同時操作3個表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于Java中三元運算(三目運算)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 重寫hashCode()和equals()方法詳細(xì)介紹

    重寫hashCode()和equals()方法詳細(xì)介紹

    這篇文章主要介紹了重寫hashCode()和equals()方法詳細(xì)介紹,涉及重寫equals()方法,重寫hashCode()方法,重寫equals()而不重寫hashCode()的風(fēng)險等相關(guān)內(nèi)容的介紹,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01

最新評論