我使用两个数据源,pg和oracle。当我使用javaconfig创建数据源时,数据源为null。
@Configuration
public class DataSourceConfig {
@Autowired
private DbConfigProperties dbConfigProperties;
@Primary
@Bean("pgDataSource")
public DataSource pgDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName(dbConfigProperties.getPgDriverClassName());
hikariDataSource.setUsername(dbConfigProperties.getPgUsername());
hikariDataSource.setPassword(dbConfigProperties.getPgPassword());
hikariDataSource.setJdbcUrl(dbConfigProperties.getPgUrl());
hikariDataSource.setIdleTimeout(dbConfigProperties.getIdleTimeout());
hikariDataSource.setConnectionTimeout(dbConfigProperties.getConnectionTimeout());
hikariDataSource.setMaximumPoolSize(dbConfigProperties.getMaximumPoolSize());
hikariDataSource.setMinimumIdle(dbConfigProperties.getMinimumIdle());
return hikariDataSource;
}
}
@Configuration
public class PgMybatisConfig {
@Autowired
@Qualifier("pgDataSource")
private DataSource pgDataSource;
@Autowired
@Qualifier("pgPageInterceptor")
private PageInterceptor pgPageInterceptor;
@Bean("pgSqlSessionFactory")
public SqlSessionFactory pgSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setTypeAliasesPackage("com.zycfc.entity.tag");
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource[]{new ClassPathResource("mapper/tag/*.xml")});
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pgPageInterceptor});
sqlSessionFactoryBean.setDataSource(pgDataSource);//here datasource is null
return sqlSessionFactoryBean.getObject();
}
}