首页 > spring boot 和mybatis多数据源配置

spring boot 和mybatis多数据源配置

@Configuration
@MapperScan(basePackages="com.infosoft.sd.mapper.mysql",sqlSessionFactoryRef="mysqlSessionFactory")
public class MysqlDataSourceConfig {
    
    @Bean(name = "mysqlDS")
    @Profile("prod")
    @Primary
    public DataSource mysqlProdDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl(
                "jdbc:mysql://xxxxxxxxxxxxx/xxxxxxx?useUnicode=true&characterEncoding=UTF-8");
        dataSource.setUser("xxxxx");
        dataSource.setPassword("xxxxxxx");
        dataSource.setAcquireIncrement(x);
        dataSource.setMaxPoolSize(xx);
        dataSource.setMinPoolSize(x);
        dataSource.setMaxIdleTime(xxx);
        return dataSource;
    }




@Configuration
@MapperScan(basePackages="com.infosoft.sd.mapper.ads",sqlSessionFactoryRef="adsSessionFactory")
public class AdsDataSourceConfig {
    
    @Bean(name = "adsDs")
    public DataSource adsDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl(
                "jdbc:mysql://xxxxxxxxx/xxxxx?useUnicode=true&characterEncoding=UTF-8");
        dataSource.setUser("xxxxxx");
        dataSource.setPassword("xxxxxxx");
        dataSource.setAcquireIncrement(x);
        dataSource.setMaxPoolSize(xx);
        dataSource.setMinPoolSize(x);
        dataSource.setMaxIdleTime(xxx);
        return dataSource;
    }

这是我配置的两个数据源,希望在该项目中同时使用这两个数据源,com.infosoft.sd.mapper.mysql包中的mapper使用mysqlSessionFactory数据源,com.infosoft.sd.mapper.ads包中的mapper使用adsSessionFactory数据源。

但是现在出现的情况是com.infosoft.sd.mapper.ads包中的mapper也是使用mysqlSessionFactory数据源。启动后出现

2016-01-26 10:07:38.675  INFO 65832 --- [pool-2-thread-1] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2016-01-26 10:07:38.768  INFO 65832 --- [pool-2-thread-1] o.s.jdbc.support.SQLErrorCodesFactory    : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]

我也遇到相同问题,每次获得@primary数据源


在sessionFactory里注入不同的数据源就可以了.
http://my.oschina.net/foreverZx/blog/674168
看下这个配置类.

指定随便一个DS为Primary
这里有个坑就是如果两个Mybatis配置的Resources都指向同一个目录会发生神奇的事情,最好每个数据源指定一个单独的路径


我想知道您是怎么控制事务的?


请问解决了吗?

【热门文章】
【热门文章】