属性(用户名,pasword,url除外)未使用@ConfigurationProperties加载



我有两个datsources,但我只能加载用户名,密码和url。其他属性,如poolPingEnabled,poolPingQuery,poolPingConnectionsNotUsedFor,poolMaximumActiveConnections。

注意:我的代码流正在工作,我能够连接到两个数据库。我的主要问题是为什么没有加载其他属性。我找不到任何有助于添加我上面提到的属性的示例。

@Configuration
    public class DatabaseConfiguration {
  public static final String GDPR_DATASOURCE = "gdpr";
  public static final String CONFIG_DATASOURCE = "config";
  @Bean(name = GDPR_DATASOURCE)
  @ConfigurationProperties(prefix = "gdpr.jdbc")
  public DataSource gdprDataSource() {
    return DataSourceBuilder.create().build();
  }
  @Bean(name = CONFIG_DATASOURCE)
  @ConfigurationProperties(prefix = "config.jdbc")
  public DataSource configDataSource() {
    return DataSourceBuilder.create().build();
  }
}

应用程序属性:用户名和密码 这里只是示例。在我的情况下,网址,用户名和密码一切都不同。

spring.datasource.initialize=false
gdpr.jdbc.url=jdbc:mysql://localhost:3306/gdpr
gdpr.jdbc.driverClassName=com.mysql.jdbc.Driver
gdpr.jdbc.username=root
gdpr.jdbc.password=local
gdpr.jdbc.poolPingEnabled=true
gdpr.jdbc.poolPingQuery=SELECT 1
gdpr.jdbc.poolPingConnectionsNotUsedFor=9000
gdpr.jdbc.poolMaximumActiveConnections=25
config.jdbc.url=jdbc:mysql://localhost:3306/config
config.jdbc.driverClassName=com.mysql.jdbc.Driver
config.jdbc.username=root
config.jdbc.password=local
config.jdbc.poolPingEnabled=true
config.jdbc.poolPingQuery=SELECT 1
config.jdbc.poolPingConnectionsNotUsedFor=10000
config.jdbc.poolMaximumActiveConnections=25

MyBatisConfiguration.java : 在这里,我正在创建两个 SqlSessionFactoryBean。这些用于联系各个数据库。

@Configuration
public class MyBatisConfiguration {
  private static final String GDPR_SESSION_FACTORY = "gdprSessionFactory";
  private static final String CONFIG_SESSION_FACTORY = "configSessionFactory";
  /** This method is used for generating SqlSessionFactoryBean for gdpr DB.
   * @param gdprDataSource gdpr datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for gdpr DB
   */
  @Bean(name = GDPR_SESSION_FACTORY, destroyMethod = "")
  @Primary
  public SqlSessionFactoryBean gdprSqlSessionFactory(
      @Named(DatabaseConfiguration.GDPR_DATASOURCE) final DataSource gdprDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(gdprDataSource);
    SqlSessionFactory sqlSessionFactory;
    sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(GdprDatabaseMapper.class);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:GdprDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }
  /** This method creates MapperFactory for SqlSessionFactoryBean GDPR_SESSION_FACTORY.
   * @param sqlSessionFactoryBean gdpr SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<GdprDatabaseMapper> gdprMapper(
  @Named(GDPR_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
    MapperFactoryBean<GdprDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(GdprDatabaseMapper.class);
    factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
    return factoryBean;
  }
  /** This method is used for generating SqlSessionFactoryBean for config DB.
   * @param configDataSource config datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for config DB
   */
  @Bean(name = CONFIG_SESSION_FACTORY, destroyMethod = "")
  public SqlSessionFactoryBean configSqlSessionFactory(
  @Named(DatabaseConfiguration.CONFIG_DATASOURCE) final DataSource configDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean =
    new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(configDataSource);
    final SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(ConfigDatabaseMapper.class);
PathMatchingResourcePatternResolver resolver = new     PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:ConfigDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }
  /** This method creates MapperFactory for SqlSessionFactoryBean CONFIG_SESSION_FACTORY.
   * @param sqlSessionFactoryBean config SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<ConfigDatabaseMapper> configMapper(
  @Named(CONFIG_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
MapperFactoryBean<ConfigDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(ConfigDatabaseMapper.class);
factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
return factoryBean;
  }
}

问题是您正在尝试使用连接池中构建的 mybatis 可以理解的配置参数。春天不知道他们。

当您使用弹簧引导时,不会使用Mybatis连接池。而是使用 Spring 中配置的连接池。

要解决此问题,您需要添加一些连接池,如此处所述。

然后,您需要通过应用程序属性配置 Spring 启动托管连接池。确切的属性取决于您使用的连接池。更多细节在这里。

例如,对于tomcat连接池,它看起来像这样:

# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true

最新更新