<i id='N2eHJ'><tr id='N2eHJ'><dt id='N2eHJ'><q id='N2eHJ'><span id='N2eHJ'><b id='N2eHJ'><form id='N2eHJ'><ins id='N2eHJ'></ins><ul id='N2eHJ'></ul><sub id='N2eHJ'></sub></form><legend id='N2eHJ'></legend><bdo id='N2eHJ'><pre id='N2eHJ'><center id='N2eHJ'></center></pre></bdo></b><th id='N2eHJ'></th></span></q></dt></tr></i><div id='N2eHJ'><tfoot id='N2eHJ'></tfoot><dl id='N2eHJ'><fieldset id='N2eHJ'></fieldset></dl></div>
    1. <small id='N2eHJ'></small><noframes id='N2eHJ'>

    2. <legend id='N2eHJ'><style id='N2eHJ'><dir id='N2eHJ'><q id='N2eHJ'></q></dir></style></legend>
        <bdo id='N2eHJ'></bdo><ul id='N2eHJ'></ul>
      1. <tfoot id='N2eHJ'></tfoot>

        springboot后端配置多个数据源、Mysql数据库的便捷方法

        时间:2023-12-07

        <i id='UoexS'><tr id='UoexS'><dt id='UoexS'><q id='UoexS'><span id='UoexS'><b id='UoexS'><form id='UoexS'><ins id='UoexS'></ins><ul id='UoexS'></ul><sub id='UoexS'></sub></form><legend id='UoexS'></legend><bdo id='UoexS'><pre id='UoexS'><center id='UoexS'></center></pre></bdo></b><th id='UoexS'></th></span></q></dt></tr></i><div id='UoexS'><tfoot id='UoexS'></tfoot><dl id='UoexS'><fieldset id='UoexS'></fieldset></dl></div>
      2. <legend id='UoexS'><style id='UoexS'><dir id='UoexS'><q id='UoexS'></q></dir></style></legend>
          <bdo id='UoexS'></bdo><ul id='UoexS'></ul>
            <tbody id='UoexS'></tbody>
            <tfoot id='UoexS'></tfoot>

              <small id='UoexS'></small><noframes id='UoexS'>

                • 下面我为大家详细介绍“Spring Boot后端配置多个数据源、MySQL数据库的便捷方法”攻略。

                  一、引入相关依赖

                  在pom.xml文件中,引入MyBatis和多数据源依赖:

                  <!--MyBatis依赖-->
                  <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.version}</version>
                  </dependency>
                  <!--多数据源依赖-->
                  <dependency>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                    <version>3.4.5</version>
                  </dependency>
                  

                  二、配置application.yml文件

                  在application.yml文件中,进行多数据源的配置,例如配置一个名为primary的数据源和一个名为secondary的数据源。

                  spring:
                    datasource:
                      primary:
                        jdbc-url: jdbc:mysql://localhost:3306/primary?characterEncoding=utf8&useSSL=false
                        username: root
                        password: 123456
                        driver-class-name: com.mysql.jdbc.Driver
                        type: com.zaxxer.hikari.HikariDataSource
                        hikari:
                          pool-name: primary-db-pool
                          minimum-idle: 5
                          maximum-pool-size: 20
                          connection-timeout: 30000
                          idle-timeout: 600000
                          max-lifetime: 1800000
                      secondary:
                        jdbc-url: jdbc:mysql://localhost:3306/secondary?characterEncoding=utf8&useSSL=false
                        username: root
                        password: 123456
                        driver-class-name: com.mysql.jdbc.Driver
                        type: com.zaxxer.hikari.HikariDataSource
                        hikari:
                          pool-name: secondary-db-pool
                          minimum-idle: 5
                          maximum-pool-size: 20
                          connection-timeout: 30000
                          idle-timeout: 600000
                          max-lifetime: 1800000
                  

                  三、创建数据源的配置类

                  根据不同的数据源,在代码中分别创建数据源的配置类,例如:

                  @Configuration
                  @EnableTransactionManagement
                  @MapperScan(basePackages = {"com.example.mapper.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
                  public class PrimaryDataSourceConfig {
                  
                    @Bean(name = "primaryDataSource")
                    @Primary
                    @ConfigurationProperties(prefix = "spring.datasource.primary")
                    public DataSource dataSource() {
                        return DataSourceBuilder.create().type(HikariDataSource.class).build();
                    }
                  
                    @Bean(name = "primarySqlSessionFactory")
                    @Primary
                    public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource,
                                                       @Qualifier("mybatisConfig") MybatisConfigurationProperties configurationProperties)
                        throws Exception {
                        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
                        sessionFactory.setDataSource(dataSource);
                        sessionFactory.setTypeHandlersPackage("com.example.mybatis.typehandler");
                        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                                .getResources("classpath:mybatis/mapper/primary/*.xml"));
                        sessionFactory.setConfiguration(configurationProperties.getConfiguration());
                        return sessionFactory.getObject();
                    }
                  
                    @Bean(name = "primaryDataSourceTransactionManager")
                    @Primary
                    public DataSourceTransactionManager transactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
                        return new DataSourceTransactionManager(dataSource);
                    }
                  }
                  

                  四、在代码中使用不同数据源

                  如需要在service层中使用另一个数据源,则需使用@Qualifier注解标明需要使用的数据源,例如:

                  @Service
                  @Transactional
                  public class UserService {
                  
                      @Autowired
                      @Qualifier("secondarySqlSessionFactory")
                      private SqlSessionFactory secondarySqlSessionFactory;
                  
                      public User getUserById(Long id) {
                          try (SqlSession sqlSession = secondarySqlSessionFactory.openSession()) {
                              UserDao userDao = sqlSession.getMapper(UserDao.class);
                              return userDao.selectById(id);
                          }
                      }
                  }
                  

                  这样,我们就可以方便地进行多数据源配置了。

                  另外,如果需要进行读写分离,可以使用ShardingSphere等中间件来实现。

                  关于在Spring Boot中使用多数据源,我写了一篇博客,里面包含了详细的代码示例:Spring Boot后端配置多个数据源、MySQL数据库的便捷方法。

                  上一篇:使用微软的webmatrix配置php网站的步骤 下一篇:NodeJs Express框架操作MongoDB数据库执行方法讲解

                  相关文章

                  <legend id='LjAVS'><style id='LjAVS'><dir id='LjAVS'><q id='LjAVS'></q></dir></style></legend>
                • <i id='LjAVS'><tr id='LjAVS'><dt id='LjAVS'><q id='LjAVS'><span id='LjAVS'><b id='LjAVS'><form id='LjAVS'><ins id='LjAVS'></ins><ul id='LjAVS'></ul><sub id='LjAVS'></sub></form><legend id='LjAVS'></legend><bdo id='LjAVS'><pre id='LjAVS'><center id='LjAVS'></center></pre></bdo></b><th id='LjAVS'></th></span></q></dt></tr></i><div id='LjAVS'><tfoot id='LjAVS'></tfoot><dl id='LjAVS'><fieldset id='LjAVS'></fieldset></dl></div>

                      <tfoot id='LjAVS'></tfoot>

                      <small id='LjAVS'></small><noframes id='LjAVS'>

                        <bdo id='LjAVS'></bdo><ul id='LjAVS'></ul>