springboot07-不同种类的多数据源整合

前言:

我们之前用的数据库是postgresql,现在要用mysql,我自己又重新建了一个mysql的,但数据要得迁移过去,所以就想到了这个多数据源的方式!

1.添加依赖:


			mysql
			mysql-connector-java
		
		
		
			com.alibaba
			druid-spring-boot-starter
			1.1.6
		
		
			com.alibaba
			druid
		
		
			org.postgresql
			postgresql
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.0
		

2.添加数据库配置,数据库1和2的前缀(lbbdatasource.druid和lbbdatasource.druid2)是我任意起的,想改用阿里的数据源,配置的lbbdatasource.druid.type没有起作用,哪位大神帮忙看看

lbbdatasource.druid.url=jdbc:postgresql://xxxxxx
lbbdatasource.druid.driver-class=org.postgresql.Driver 
lbbdatasource.druid.username=xxxx
lbbdatasource.druid.password=xxx
#没有起作用 
lbbdatasource.druid.type=com.alibaba.druid.pool.DruidDataSource 

lbbdatasource.druid2.url=jdbc:mysql://xxxxxxxx?useSSL=false&useUnicode=true&characterEncoding=utf-8 
lbbdatasource.druid2.driver-class=com.mysql.jdbc.Driver 
lbbdatasource.druid2.username=xx
lbbdatasource.druid2.password=xxxx
#没有起作用 
lbbdatasource.druid2.type=com.alibaba.druid.pool.DruidDataSource

3.更改数据连接,要注意的是@Primary注释只能在一个数据库中配置,不然系统无法分清哪个是主库,导入用import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.lbb.db1", sqlSessionTemplateRef  = "db1SqlSessionTemplate")
public class DbOneConfig {
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "lbbdatasource.druid")
    @Primary
    public DataSource testDataSource() {
        return  DataSourceBuilder.create().build();
    }

    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatisOne/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
@MapperScan(basePackages = "com.lbb.db2", sqlSessionTemplateRef  = "db2SqlSessionTemplate")
public class DbTwoConfig {
    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "lbbdatasource.druid2")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatisTwo/*.xml"));
        return bean.getObject();
    }


    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

4.controller访问(dao层自己任意建一个,重点不在这里),每次访问接口的时候,打印一下看看是哪个driver

 @Autowired
    private DataSource dataSource;

    List< Map> listmap = new ArrayList< Map>();

    @RequestMapping("getValue")
    public String getValue() throws SQLException {
         listmap=dbOneDao.getValue();

        Connection connection = dataSource.getConnection();
        System.out.println("@@@@接口getValue访问数据的类型:"+connection);
        return "success";
    }

    @RequestMapping("setValue")
    public String setValue() throws SQLException{
       dbTwoDao.setValue(listmap);
        Connection connection = dataSource.getConnection();
        System.out.println("@@@@接口getValue访问数据的类型:"+connection);
        return "success";
    }

完成!

遗留问题:

我想改用阿里巴巴的连接池,lbbdatasource.druid.type没有起作用,网上有说springboot默认不支持阿里的,得配置


			org.springframework.boot
			spring-boot-configuration-processor
			true
		

可依然没有解决问题,哪位大神帮忙看看!

参考:纯洁的微笑,mybaits多数据源问题

你可能感兴趣的:(springboot问题总结)