我有两个datsources但我只能加载用户名,密码和网址。其他属性如poolPingEnabled,poolPingQuery,poolPingConnectionsNotUsedFor,poolMaximumActiveConnections。
注意:我的代码流正常运行,我可以连接到两个DB。我的主要问题是为什么其他属性没有被加载。我找不到任何有助于添加上面提到的属性的例子。
@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();
}
}
Application.properties:这里的用户名和密码仅为示例。在我的情况下,网址,用户名和密码一切都不一样。
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。这些用于联系各个DB。
@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;
}
}
答案 0 :(得分:0)
问题是您正在尝试使用mybatis在连接池中构建的配置参数。春天不知道他们。
使用spring boot时不使用Mybatis连接池。而是使用在spring中配置的连接池。
要解决此问题,您需要按照here所述添加一些连接池。
然后,您需要通过应用程序属性配置Spring Boot托管连接池。确切的属性取决于您使用的连接池。更多详细信息为here。
对于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