@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数据源