SpringBoot 配置mysql和Greenplum多数据源连接
Greenplum引用maven
链接: greenplum-jdbc-5.1.4.jar. 提取码:ctwg
<!--greenplum连接 本地jar-->
<dependency>
<groupId>com.pivotal</groupId>
<artifactId>greenplum-jdbc</artifactId>
<version>5.1.4</version>
<scope>system</scope>
<systemPath>${basedir}/lib/greenplum-jdbc-5.1.4.jar</systemPath><!--项目根目录下的lib文件夹下-->
</dependency>
yml配置文件,两套配置驱动不一样
spring:
datasource:
name: ShiroJwt
url: jdbc:mysql://127.0.0.1:3306/mysql?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
druid:
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
gpdatasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.pivotal.jdbc.GreenplumDriver
url: jdbc:pivotal:greenplum://127.0.0.1:5432;DatabaseName=dcdb
username: dbtest
password: dbtest
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 30000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select version()
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 20
max-pool-prepared-statement-per-connection-size: 20
加载配置文件信息返回Datasource
@Configuration
public class DatasourceConfig {
@Bean(name = "Datasource")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "gpDatasource")
@ConfigurationProperties(prefix = "spring.gpdatasource")
public DataSource dataSourceGp() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
}
加载Bean工厂
第一个Mysql数据源
@Configuration
@MapperScan(basePackages = {"com.demo.**.mapper"}, sqlSessionFactoryRef = "mysqlSessionFactoryBean")
public class MybatisConfig {
@Autowired
@Qualifier("Datasource")
private DataSource DB;
@Primary
@Bean(name = "mysqlSessionFactoryBean")
public SqlSessionFactoryBean mysqlSessionFactoryBean() {
SqlSessionFactoryBean mysqlSessionFactoryBean = new SqlSessionFactoryBean();
mysqlSessionFactoryBean.setDataSource(DB);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return mysqlSessionFactoryBean;
}
@Primary
@Bean(name = "mysqlTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(DB);
}
}
第二个Greenplum数据源
@Configuration
@MapperScan(basePackages = {"com.demo.*.gpmapper"}, sqlSessionFactoryRef = "gpSqlSessionFactory")
public class MybatisGpConfig {
@Autowired
@Qualifier("gpDatasource")
private DataSource gpDB;
@Bean(name = "gpSqlSessionFactory")
public SqlSessionFactoryBean gpSqlSessionFactory() {
SqlSessionFactoryBean gpSqlSessionFactory = new SqlSessionFactoryBean();
gpSqlSessionFactory.setDataSource(gpDB);
return gpSqlSessionFactory;
}
@Bean(name = "gpTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(gpDB);
}
}
踩坑
@Primary 优先方案,被注解的实现,优先被注入
@Qualifier 先声明后使用,相当于多个实现起多个不同的名字,注入时候告诉我你要注入哪个
不加@Primary 会报错
required a single bean, but 2 were found:
按照上面配置完后启动 依然报错
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userRealm’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.dacheng.common.mapper.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方案 在启动类上再加上mapper扫描
@MapperScan(basePackages = {"com.demo.*.mapper"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScreenApplication.class, args);
}
}