zoukankan      html  css  js  c++  java
  • springboot-mybatis双数据源配置

    yml文件

    spring:    
      datasource:
          test1:
            driverClassName: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true
            username: root
            password: root
          test2:
            driverClassName: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true
            username: root
            password: root

    主数据源配置(必须指定主数据源:用@Primary 指定)

    @Configuration
    @MapperScan(basePackages = "com.boot.dao.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
    public class DataSource1Config {
    
        @Bean(name = "test1DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test1")
        @Primary
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "test1SqlSessionFactory")
        @Primary
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "test1TransactionManager")
        @Primary
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "test1SqlSessionTemplate")
        @Primary
        public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    
    }

    其他数据源配置

    @Configuration
    @MapperScan(basePackages = "com.boot.dao.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
    public class DataSource2Config {
    
        @Bean(name = "test2DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test2")
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "test2SqlSessionFactory")
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "test2TransactionManager")
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "test2SqlSessionTemplate")
        public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    
    }

    OK,配置完成;

    下面是测试dome

    UserMapper,UserMapper2

    List<Map<String,Object>> findAll();

    UserMapper.xml,UserMapper2.xml是不同数据源的sql,

    访问不同的Mapper可以发现访问不同的数据库

  • 相关阅读:
    greenplum 常用整理
    postgresql 常用整理
    数据源整理
    hive 以like方式建表(携带表结构)
    kudu 常用整理
    mysql常用整理
    Linux查看文件内容的常用方式(more,less,head,tail,cat)
    python2.7 安装docx错误
    python2.7 安装搜索引擎错误
    mysql数据库安装错误
  • 原文地址:https://www.cnblogs.com/changhai/p/8891190.html
Copyright © 2011-2022 走看看