<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      參考網址:http://www.rzrgm.cn/java-zhao/p/5413845.html

       

      1.配置文件application.properties中增加其他數據源設置
      #the first datasource
      jdbc.driverClassName = com.mysql.jdbc.Driver
      jdbc.username = root
      jdbc.password = 123
       
      #the second datasource
      jdbc2.driverClassName = com.mysql.jdbc.Driver
      jdbc2.username = root
      jdbc2.password = 123
       
       
      2.建立databaseType枚舉類,列舉數據源的key
      package com.xxx.firstboot.common.datasource;
       
      /**
      * 列出所有的數據源key(常用數據庫名稱來命名)
      * 注意:
      * 1)這里數據源與數據庫是一對一的
      * 2)DatabaseType中的變量名稱就是數據庫的名稱
      */
      public enum DatabaseType {
          mytestdb,mytestdb2
      }
      

        

       
      3.DatabaseContextHolder構建DatabaseType容器,并提供了向其中設置和獲取DatabaseType的方法
      package com.xxx.firstboot.common.datasource;
       
      /**
      * 作用:
      * 1、保存一個線程安全的DatabaseType容器
      */
      public class DatabaseContextHolder {
          private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<>();
          
          public static void setDatabaseType(DatabaseType type){
              contextHolder.set(type);
          }
          
          public static DatabaseType getDatabaseType(){
              return contextHolder.get();
          }
      }
      

       

      4.建立DynamicDataSource類,使用DatabaseContextHolder獲取當前線程的DatabaseType
      package com.xxx.firstboot.common.datasource;
       
      import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
       
      /**
      * 動態數據源(需要繼承AbstractRoutingDataSource)
      */
      public class DynamicDataSource extends AbstractRoutingDataSource {
          protected Object determineCurrentLookupKey() {
              return DatabaseContextHolder.getDatabaseType();
          }
      }
      

        

       
      5.配置MyBatisConfig,通過application.properties文件生成兩個數據源,使用這兩個數據源構造動態的dataSource
      @Primary:指定在同一個接口有多個實現類可以注入的時候,默認選擇哪一個
      @Qualifier:指定名稱的注入,當一個接口有多個實現類時使用
      @Bean:生成的bean示例的名稱
      package com.xxx.firstboot.common;
       
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Properties;
       
      import javax.sql.DataSource;
       
      import org.apache.ibatis.session.SqlSessionFactory;
      import org.mybatis.spring.SqlSessionFactoryBean;
      import org.mybatis.spring.annotation.MapperScan;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Primary;
      import org.springframework.core.env.Environment;
      import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
      import org.springframework.jdbc.datasource.DataSourceTransactionManager;
       
      import com.alibaba.druid.pool.DruidDataSourceFactory;
      import com.xxx.firstboot.common.datasource.DatabaseType;
      import com.xxx.firstboot.common.datasource.DynamicDataSource;
       
      /**
      * springboot集成mybatis的基本入口 1)創建數據源(如果采用的是默認的tomcat-jdbc數據源,則不需要)
      * 2)創建SqlSessionFactory 3)配置事務管理器,除非需要使用事務,否則不用配置
      */
      @Configuration // 該注解類似于spring配置文件
      @MapperScan(basePackages = "com.xxx.firstboot.mapper")
      public class MyBatisConfig {
       
          @Autowired
          private Environment env;
       
          /**
           * 創建數據源(數據源的名稱:方法名可以取為XXXDataSource(),XXX為數據庫名稱,該名稱也就是數據源的名稱)
           */
          @Bean
          public DataSource myTestDbDataSource() throws Exception {
              Properties props = new Properties();
              props.put("driverClassName", env.getProperty("jdbc.driverClassName"));
              props.put("url", env.getProperty("jdbc.url"));
              props.put("username", env.getProperty("jdbc.username"));
              props.put("password", env.getProperty("jdbc.password"));
              return DruidDataSourceFactory.createDataSource(props);
          }
       
          @Bean
          public DataSource myTestDb2DataSource() throws Exception {
              Properties props = new Properties();
              props.put("driverClassName", env.getProperty("jdbc2.driverClassName"));
              props.put("url", env.getProperty("jdbc2.url"));
              props.put("username", env.getProperty("jdbc2.username"));
              props.put("password", env.getProperty("jdbc2.password"));
              return DruidDataSourceFactory.createDataSource(props);
          }
       
          /**
           * @Primary 該注解表示在同一個接口有多個實現類可以注入的時候,默認選擇哪一個,而不是讓@autowire注解報錯
           * @Qualifier 根據名稱進行注入,通常是在具有相同的多個類型的實例的一個注入(例如有多個DataSource類型的實例)
           */
          @Bean
          @Primary
          public DynamicDataSource dataSource(@Qualifier("myTestDbDataSource") DataSource myTestDbDataSource,
                  @Qualifier("myTestDb2DataSource") DataSource myTestDb2DataSource) {
              Map<Object, Object> targetDataSources = new HashMap<>();
              targetDataSources.put(DatabaseType.mytestdb, myTestDbDataSource);
              targetDataSources.put(DatabaseType.mytestdb2, myTestDb2DataSource);
       
              DynamicDataSource dataSource = new DynamicDataSource();
              dataSource.setTargetDataSources(targetDataSources);// 該方法是AbstractRoutingDataSource的方法
              dataSource.setDefaultTargetDataSource(myTestDbDataSource);// 默認的datasource設置為myTestDbDataSource
       
              return dataSource;
          }
       
          /**
           * 根據數據源創建SqlSessionFactory
           */
          @Bean
          public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds) throws Exception {
              SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
              fb.setDataSource(ds);// 指定數據源(這個必須有,否則報錯)
              // 下邊兩句僅僅用于*.xml文件,如果整個持久層操作不需要使用到xml文件的話(只用注解就可以搞定),則不加
              fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));// 指定基包
              fb.setMapperLocations(
                      new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));//
       
              return fb.getObject();
          }
       
          /**
           * 配置事務管理器
           */
          @Bean
          public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
              return new DataSourceTransactionManager(dataSource);
          }
       
      }
      

        

       
      6.使用實例
      package com.xxx.firstboot.web;
       
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;
       
      import com.xxx.firstboot.domain.Shop;
      import com.xxx.firstboot.service.ShopService;
       
      import io.swagger.annotations.Api;
      import io.swagger.annotations.ApiOperation;
       
      @RestController
      @RequestMapping("/shop")
      @Api("shopController相關api")
      public class ShopController {
       
          @Autowired
          private ShopService service;
       
          @ApiOperation("獲取shop信息,測試多數據源")
          @RequestMapping(value = "/getShop", method = RequestMethod.GET)
          public Shop getShop(@RequestParam("id") int id) {
              return service.getShop(id);
          }
       
      }
      

       

      package com.xxx.firstboot.service;
       
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
       
      import com.xxx.firstboot.dao.ShopDao;
      import com.xxx.firstboot.domain.Shop;
       
      @Service
      public class ShopService {
       
          @Autowired
          private ShopDao dao;
       
          public Shop getShop(int id) {
              return dao.getShop(id);
          }
      }
       
      package com.xxx.firstboot.dao;
       
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Repository;
       
      import com.xxx.firstboot.common.datasource.DatabaseContextHolder;
      import com.xxx.firstboot.common.datasource.DatabaseType;
      import com.xxx.firstboot.domain.Shop;
      import com.xxx.firstboot.mapper.ShopMapper;
       
      @Repository
      public class ShopDao {
          @Autowired
          private ShopMapper mapper;
       
          /**
           * 獲取shop
           */
          public Shop getShop(int id) {
              DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
              return mapper.getShop(id);
          }
      }
      

        

       
       
      package com.xxx.firstboot.mapper;
       
      import org.apache.ibatis.annotations.Param;
      import org.apache.ibatis.annotations.Result;
      import org.apache.ibatis.annotations.Results;
      import org.apache.ibatis.annotations.Select;
       
      import com.xxx.firstboot.domain.Shop;
       
      public interface ShopMapper {
       
          @Select("SELECT * FROM t_shop WHERE id = #{id}")
          @Results(value = { @Result(id = true, column = "id", property = "id"),
                             @Result(column = "shop_name", property = "shopName") })
          public Shop getShop(@Param("id") int id);
       
      }
      

        

       
      7.通過AOP實現數據源切換
       
      package com.xxx.firstboot.dao;
       
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Repository;
       
      import com.xxx.firstboot.domain.Shop;
      import com.xxx.firstboot.mapper.ShopMapper;
       
      @Repository
      public class ShopDao {
          @Autowired
          private ShopMapper mapper;
       
          /**
           * 獲取shop
           */
          public Shop getShop(int id) {
              return mapper.getShop(id);
          }
      }
      

        

       
      package com.xxx.firstboot.common.datasource;
       
      import org.aspectj.lang.JoinPoint;
      import org.aspectj.lang.annotation.Aspect;
      import org.aspectj.lang.annotation.Before;
      import org.springframework.stereotype.Component;
       
      import com.xxx.firstboot.dao.ShopDao;
       
      @Aspect
      @Component
      public class DataSourceAspect {
          
          @Before("execution(* com.xxx.firstboot.dao.*.*(..))")
          public void setDataSourceKey(JoinPoint point){
              //連接點所屬的類實例是ShopDao
              if(point.getTarget() instanceof ShopDao){
                  DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
              }else{//連接點所屬的類實例是UserDao(當然,這一步也可以不寫,因為defaultTargertDataSource就是該類所用的mytestdb)
                  DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
              }
          }
          
      //    @Around("execution(* com.xxx.firstboot.dao.*.*(..))")
      //    public Object setDataSourceKeyByAround(ProceedingJoinPoint point) throws Throwable{
      //        if(point.getTarget() instanceof ShopDao){
      //            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
      //        }else{//連接點所屬的類實例是UserDao(當然,這一步也可以不寫,因為defaultTargertDataSource就是該類所用的mytestdb)
      //            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
      //        }
      //        return point.proceed();//執行目標方法
      //    }
          
      }
      

        

       
      或者
      package com.xxx.firstboot.common.datasource;
       
      import org.aspectj.lang.JoinPoint;
      import org.aspectj.lang.annotation.Aspect;
      import org.aspectj.lang.annotation.Before;
      import org.aspectj.lang.annotation.Pointcut;
      import org.springframework.stereotype.Component;
       
      import com.xxx.firstboot.dao.ShopDao;
       
      @Aspect
      @Component
      public class DataSourceAspect {
       
          /**
           * 使用空方法定義切點表達式
           */
          @Pointcut("execution(* com.xxx.firstboot.dao.*.*(..))")
          public void declareJointPointExpression() {
          }
       
          /**
           * 使用定義切點表達式的方法進行切點表達式的引入
           */
          @Before("declareJointPointExpression()")
          public void setDataSourceKey(JoinPoint point) {
              // 連接點所屬的類實例是ShopDao
              if (point.getTarget() instanceof ShopDao) {
                  DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
              } else {// 連接點所屬的類實例是UserDao(當然,這一步也可以不寫,因為defaultTargertDataSource就是該類所用的mytestdb)
                  DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
              }
          }
       
      }
      

        

       
      posted on 2018-12-22 16:02  梓木的稻稻  閱讀(545)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 久久精品蜜芽亚洲国产av| 色综合久久婷婷88| 一边捏奶头一边高潮视频| 无码av岛国片在线播放| 一本色道久久88亚洲精品综合 | 国产成人无码午夜视频在线播放| 狼色精品人妻在线视频| 狠狠色婷婷久久综合频道日韩| 久久精品女人天堂av免费观看| 合江县| 亚洲国产亚洲国产路线久久| 成人免费乱码大片a毛片| 粉嫩在线一区二区三区视频| 亚洲另类激情专区小说婷婷久| 黄色免费在线网址| 日本乱码在线看亚洲乱码| 亚洲中文在线精品国产| 国产一卡2卡三卡4卡免费网站| 欧美激欧美啪啪片| 久久精品免视看国产成人| 国产999精品2卡3卡4卡| 国产精品高清一区二区三区不卡 | 2018av天堂在线视频精品观看| 亚洲aⅴ男人的天堂在线观看| 粉嫩av蜜臀一区二区三区| 国产私拍大尺度在线视频| 久久日韩在线观看视频| 国产农村老熟女乱子综合| 少妇人妻偷人精品免费| 亚洲欧美人成电影在线观看| 精品中文人妻在线不卡| 久久99精品久久久久久| 久久婷婷五月综合色欧美| 男人又大又硬又粗视频| 日本一区不卡高清更新二区 | 亚洲国产福利成人一区二区| 亚洲人成电影网站 久久影视| 国产国语一级毛片| 日韩美av一区二区三区| 国产一区在线播放av| 翘臀少妇被扒开屁股日出水爆乳|