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);
	}
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestJDBC {       private Connection con ;       private String user = "gpadmin" ;       private String password = "123456" ;       private String className = "com.pivotal.jdbc.GreenplumDriver" ;       private String url = "jdbc:pivotal:greenplum://172.20.4.161:5432;DatabaseName=testDB" ;          public static void main(String[] args) {              // TODO Auto-generated method stub             TestJDBC c = new TestJDBC();              c.Connect();              c.getCon();              c.closed();      }       public void Connect() {              try {                    Class. forName(className);                    System. out.println("加载数据库驱动成功!" );             } catch (ClassNotFoundException e ) {                    System. out.println("加载数据库驱动失败!" );                     e.printStackTrace();             }      }       /** 创建数据库连接 */       public Connection getCon() {              try {                     con = DriverManager. getConnection(url, user, password);                    System. out.println("创建数据库连接成功!" );             } catch (SQLException e ) {                    System. out.print(con );                    System. out.println("创建数据库连接失败!" );                     con = null;                     e.printStackTrace();             }              return con ;      }       public void closed() {              try {                     if (con != null) {                           con.close();                    }             } catch (SQLException e ) {                    System. out.println("关闭con对象失败!" );                     e.printStackTrace();             }      }   }  
Spring Boot是一个用于创建独立的、可扩展的Java应用程序的开发框架,而Greenplum是一个用于大数据分析的强大的关系型数据库。下面是使用Spring Boot连接Greenplum数据库的步骤: 1. 添加依赖:在项目的pom.xml文件中添加相应的依赖项,包括Greenplum JDBC驱动和Spring Boot JDBC依赖。例如: ``` <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>最新版本号</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置数据库连接:在Spring Boot配置文件(application.properties或application.yml)中添加数据库连接参数,包括数据库的URL、用户名和密码等。例如: ``` spring.datasource.url=jdbc:postgresql://数据库地址:端口号/数据库名 spring.datasource.username=用户名 spring.datasource.password=密码 ``` 3. 创建实体类:在Java代码中创建与数据库表对应的实体类,并使用JPA注解标识实体类与表的映射关系。 4. 创建数据访问接口:创建一个继承自JpaRepository的接口,用于定义数据库操作的方法。 5. 编写业务逻辑:在Service层编写业务逻辑代码,调用数据访问接口中定义的方法对数据库进行操作。 通过以上步骤,我们可以使用Spring Boot连接和操作Greenplum数据库。可以使用Greenplum数据库的强大功能进行数据分析和处理,同时借助Spring Boot的便捷特性开发出高效可靠的应用程序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值