zoukankan      html  css  js  c++  java
  • SpringBoot中基于Mybatis多数据源操作

    在SpringBoot中,基于Mybatis多数据源操作,需要以下配置:

    1、在项目的pom.xml 文件中配置多个数据库的连接配置信息

    2、根据数据源的数量编写多个数据库的配置文件xxxxConfig

    导入的包

    import com.alibaba.druid.pool.DruidDataSource;
    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.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    

      

    @Configuration
    // 扫描 Mapper 接口并容器管理
    @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
    public class MasterDataSourceConfig {
      //精确到 master 目录,以便跟其他数据源隔离
      static final String PACKAGE = "com.xxx.xxx.xxx.dao.master";
      static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
    
      @Value("${spring.datasource.master.url}")
      private String url;
    
      @Value("${spring.datasource.master.username}")
      private String user;
    
      @Value("${spring.datasource.master.password}")
      private String password;
    
      @Value("${spring.datasource.master.driver-class-name}")
      private String driverClassName;
    
      @Bean(name = "masterDataSource")
      @Primary
      public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
      }
    
      @Bean(name = "masterTransactionManager")
      @Primary
      public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
      }
    
      @Bean(name = "masterSqlSessionFactory")
      @Primary
      public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
          throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
      }
    }
    

      

    @Configuration
    // 扫描 Mapper 接口并容器管理
    @MapperScan(basePackages = SecondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
    public class SecondDataSourceConfig {
      // 精确到 cluster 目录,以便跟其他数据源隔离
      static final String PACKAGE = "com.xxx.xxx.xxx.dao.second";
      static final String MAPPER_LOCATION = "classpath:mapper/second/*.xml";
    
      @Value("${spring.datasource.second.url}")
      private String url;
    
      @Value("${spring.datasource.second.username}")
      private String user;
    
      @Value("${spring.datasource.second.password}")
      private String password;
    
      @Value("${spring.datasource.second.driver-class-name}")
      private String driverClass;
    
      @Bean(name = "secondDataSource")
      public DataSource clusterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
      }
    
      @Bean(name = "secondTransactionManager")
      public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager(clusterDataSource());
      }
    
      @Bean(name = "secondSqlSessionFactory")
      public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("secondDataSource") DataSource clusterDataSource)
          throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clusterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources(SecondDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
      }
    }
    

      

  • 相关阅读:
    阻止所有普通账号登录服务器
    grep命令
    redis集群配置和节点管理
    redis的安装和配置
    shell特殊字符
    获取docker容器的ip地址
    mysql5.6.38误删除root用户后的一系列问题及解决办法
    CenrOS7.5安装msyql5.7.24
    使用163邮箱的smtp ssl端口发送邮件
    linux统计大文件行数的命令效率大对比
  • 原文地址:https://www.cnblogs.com/MoreThinking/p/13652074.html
Copyright © 2011-2022 走看看