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

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

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

      使用jdbcTemplate查詢數據庫

      springboot2版本項目中已經整合了mybatis框架,yml文件中配置好了數據源, 現在想再使用jdbcTemplate查詢另外一個數據庫,需要怎么配置

      image

       

      # 這是你現有的MyBatis數據源配置(假設使用默認前綴)
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/main_db
          username: root
          password: password
          driver-class-name: com.mysql.cj.jdbc.Driver
      
        # 新增的第二個數據源配置
        datasource:
          secondary:
            jdbc-url: jdbc:mysql://localhost:3306/another_db # 注意這里使用jdbc-url而非url:cite[5]:cite[10]
            username: root
            password: password
            driver-class-name: com.mysql.cj.jdbc.Driver
            # 可以根據需要配置連接池參數,例如HikariCP:
            hikari:
              maximum-pool-size: 10
              minimum-idle: 2

      注意:第二個數據源的URL配置,建議使用 jdbc-url 而不是 url。這是因為Spring Boot在自動配置多個數據源時可能會產生沖突,明確指定 jdbc-url 可以避免 "jdbcUrl is required with driverClassName" 的錯誤

      編寫Java配置類

      新建一個Java配置類(例如 JdbcTemplateConfig),用于創建第二個數據源的 DataSource Bean 和 JdbcTemplate Bean。

      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.boot.context.properties.ConfigurationProperties;
      import org.springframework.boot.jdbc.DataSourceBuilder;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Primary;
      import org.springframework.jdbc.core.JdbcTemplate;
      
      import javax.sql.DataSource;
      
      @Configuration
      public class JdbcTemplateConfig {
      
          /**
           * 配置第二個數據源
           * 使用@ConfigurationProperties注解綁定配置文件中"spring.datasource.secondary"開頭的屬性
           */
          @Bean(name = "secondaryDataSource")
          @ConfigurationProperties(prefix = "spring.datasource.secondary")
          public DataSource secondaryDataSource() {
              return DataSourceBuilder.create().build();
          }
      
          /**
           * 創建第二個JdbcTemplate,并注入名為"secondaryDataSource"的數據源。
           * 使用@Qualifier注解明確指定要注入的Bean名稱。
           */
          @Bean(name = "secondaryJdbcTemplate")
          public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
              return new JdbcTemplate(dataSource);
          }
      }

      關鍵點說明:

      • @Bean(name = "secondaryDataSource"): 為Bean定義一個名稱,便于后續注入時區分。

      • @ConfigurationProperties(prefix = "spring.datasource.secondary"): 告訴Spring將配置文件中以 spring.datasource.secondary 為前綴的屬性映射到這個 DataSource 的所有屬性上。

      • @Qualifier("secondaryDataSource"): 當注入 DataSource 時,通過名稱明確指定要注入的是我們剛剛定義的第二個數據源Bean,而不是MyBatis使用的那個默認數據源

       

      在Service中使用第二個JdbcTemplate

      在你的Service類中,使用 @Autowired 和 @Qualifier 注解注入指定的 JdbcTemplate Bean。

       
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.jdbc.core.JdbcTemplate;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      import java.util.Map;
      
      @Service
      public class YourService {
      
          // 注入第二個JdbcTemplate
          @Autowired
          @Qualifier("secondaryJdbcTemplate") // 指定注入名為"secondaryJdbcTemplate"的Bean
          private JdbcTemplate secondaryJdbcTemplate;
      
          public List<Map<String, Object>> queryFromOtherDatabase() {
              String sql = "SELECT * FROM some_table";
              // 使用secondaryJdbcTemplate執行查詢,操作的就是另一個數據庫
              return secondaryJdbcTemplate.queryForList(sql);
          }
      }

      重要的注意事項

      1. 事務管理:需要特別注意事務問題。如果你在使用了 @Transactional 注解的方法中操作多個數據源,默認的事務管理器可能只管理其中一個數據源的事務。你需要為每個數據源配置獨立的 PlatformTransactionManager Bean,并在 @Transactional 注解中通過 transactionManager 屬性指定使用哪個事務管理器。例如:

        @Bean(name = "secondaryTransactionManager")
        public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
        }

      然后在使用時:

      @Transactional(transactionManager = "secondaryTransactionManager") // 指定事務管理器
      public void updateInSecondaryDatabase() {
      // ... 使用secondaryJdbcTemplate執行更新操作
      }

       

      對于需要跨數據源的真正分布式事務,需要考慮使用JTA解決方案,但這會復雜得多

       
      <!-- MyBatis Spring Boot Starter -->
              <dependency>
                  <groupId>org.mybatis.spring.boot</groupId>
                  <artifactId>mybatis-spring-boot-starter</artifactId>
                  <version>2.1.4</version>
              </dependency>
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <scope>runtime</scope>
              </dependency>

       

       如果遇到數據源錯亂問題,參考????

      ?? 原因分析與解決方案

      1. 數據源(DataSource)Bean的注入問題

      Spring Boot的自動配置在遇到多個DataSource Bean時,如果未明確指定,可能會注入錯誤的Bean。

      • 原因:你可能配置了多個DataSource Bean(例如一個給MyBatis,一個給JdbcTemplate),但沒有明確地通過@Primary注解指定哪個是默認數據源,或者沒有在注入時通過@Qualifier注解按名稱(Bean Name)進行區分。這會導致Spring自動注入時發生混亂。

      • 解決方案:確保每個DataSource Bean都有唯一的名稱,并且為MyBatis使用的數據源設置@Primary注解(如果你的MyBatis期望使用主數據源)。同時,在注入時使用@Qualifier明確指定。

      @Configuration
      public class DataSourceConfig {
      
          // 為MyBatis配置主數據源(primary)
          @Bean(name = "mybatisDataSource")
          @Primary // 關鍵:標記為默認數據源,MyBatis通常會使用默認的DataSource
          @ConfigurationProperties(prefix = "spring.datasource.mybatis")
          public DataSource mybatisDataSource() {
              return DataSourceBuilder.create().build();
          }
      
          // 為JdbcTemplate配置另一個數據源
          @Bean(name = "jdbcTemplateDataSource")
          @ConfigurationProperties(prefix = "spring.datasource.jdbctemplate")
          public DataSource jdbcTemplateDataSource() {
              return DataSourceBuilder.create().build();
          }
      }

      然后在配置JdbcTemplate時,明確指定使用哪個數據源:

      @Bean
      public JdbcTemplate jdbcTemplate(@Qualifier("jdbcTemplateDataSource") DataSource dataSource) {
          return new JdbcTemplate(dataSource);
      }

      MyBatis(尤其是通過mybatis-spring-boot-starter自動配置時)通常會自動使用標記了@PrimaryDataSource

      2. MyBatis配置未正確關聯指定數據源

      如果你為MyBatis自定義了配置,需要確保其使用的SqlSessionFactoryBean關聯到了正確的DataSource Bean。

      • 原因:自定義的SqlSessionFactoryBean可能沒有注入你希望它使用的DataSource

      • 解決方案:在創建SqlSessionFactoryBean時,通過@Qualifier注入為MyBatis準備的特定數據源。

      @Configuration
      @MapperScan(basePackages = "com.example.mapper", sqlSessionFactoryRef = "mybatisSqlSessionFactory")
      public class MyBatisConfig {
      
          @Bean(name = "mybatisSqlSessionFactory")
          public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("mybatisDataSource") DataSource dataSource) throws Exception {
              SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
              sessionFactory.setDataSource(dataSource); // 關聯MyBatis專用的數據源
              // 其他MyBatis配置(如mapper.xml位置等)
              return sessionFactory;
          }
      }

      3. 事務管理器(TransactionManager)沖突

      Spring的事務管理也需要知道操作哪個數據源。

      • 原因:如果只配置了一個全局的PlatformTransactionManager,它可能綁定到了錯誤的數據源上。

      • 解決方案:為不同的數據源配置各自的事務管理器,并在使用@Transactional注解時根據需要指定事務管理器(但通常查詢操作不涉及事務,此問題影響較小)。

      @Bean(name = "mybatisTransactionManager")
      @Primary
      public PlatformTransactionManager mybatisTransactionManager(@Qualifier("mybatisDataSource") DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }
      
      @Bean(name = "jdbcTemplateTransactionManager")
      public PlatformTransactionManager jdbcTemplateTransactionManager(@Qualifier("jdbcTemplateDataSource") DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }

      4. 配置文件(application.yml)問題

      確保你的配置文件中的多數據源配置前綴正確,并且沒有相互覆蓋。

      spring:
        datasource:
          # MyBatis 使用的數據源配置
          mybatis:
            jdbc-url: jdbc:mysql://localhost:3306/db_mybatis
            username: user_mybatis
            password: pass_mybatis
            driver-class-name: com.mysql.cj.jdbc.Driver
          # JdbcTemplate 使用的數據源配置
          jdbctemplate:
            jdbc-url: jdbc:mysql://localhost:3306/db_jtemplate # 注意此處使用 jdbc-url 而非 url:cite[1]
            username: user_jtemplate
            password: pass_jtemplate
            driver-class-name: com.mysql.cj.jdbc.Driver

      核心檢查點

      1. Bean名稱與限定:檢查是否為每個DataSourceJdbcTemplateSqlSessionFactoryBean等定義了清晰的Bean名稱,并在注入時使用@Qualifier

      2. @Primary注解:通常將MyBatis使用的數據源設為@Primary,因為許多Spring Boot自動配置默認期望存在一個主數據源。

      3. 配置隔離:在application.yml中使用不同的前綴(如spring.datasource.mybatisspring.datasource.jdbctemplate)清晰隔離兩個數據源的配置。

      4. URL屬性:在多數據源配置中,使用jdbc-url而非url來避免潛在的配置解析問題

       

      posted @ 2025-09-21 21:53  iTao0128  閱讀(19)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 精品国产乱码久久久久APP下载| 吴桥县| 双鸭山市| 国产精品国产三级国快看| 国内不卡的一区二区三区| 免费无码一区二区三区蜜桃大| 无码日韩av一区二区三区| 野花香视频在线观看免费高清版 | 最新国产精品亚洲| 久久午夜夜伦鲁鲁片免费无码| 婷婷成人丁香五月综合激情| 国产久免费热视频在线观看| 亚洲熟少妇在线播放999| 曝光无码有码视频专区| jizzjizz日本高潮喷水| 精精国产xxxx视频在线 | 和龙市| 97久久久亚洲综合久久| 国内永久福利在线视频图片| 攀枝花市| 成人3d动漫一区二区三区| 亚洲一二区制服无码中字| 国产精品一区二区中文| 国产乱色国产精品免费视频| 亚洲AV片一区二区三区| 亚洲色大成网站WWW久久| 色老头在线一区二区三区| 免费无码又爽又刺激网站直播| 亚洲欧洲色图片网站| 成人午夜福利精品一区二区| 国产av成人精品播放| 欧美丰满熟妇性xxxx| 色欲综合久久中文字幕网| 牛牛视频一区二区三区| 亚洲老妇女亚洲老熟女久| 国产成AV人片久青草影院| 靖远县| 一道本AV免费不卡播放| 亚洲av首页在线| 亚洲精品国产一二三区| 国产精品揄拍一区二区久久|