2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
- @ConfigurationProperties(prefix = "hive")
- @Data
- public class HiveDruidConfig {
-
- private String url;
- private String user;
- private String password;
- private String driverClassName;
- private int initialSize;
- private int minIdle;
- private int maxActive;
- private int maxWait;
- private int timeBetweenEvictionRunsMillis;
- private int minEvictableIdleTimeMillis;
- private String validationQuery;
- private boolean testWhileIdle;
- private boolean testOnBorrow;
- private boolean testOnReturn;
- private boolean poolPreparedStatements;
- private int maxPoolPreparedStatementPerConnectionSize;
-
- @Bean(name = "hiveDruidDataSource")
- @Qualifier("hiveDruidDataSource")
- public DataSource dataSource() {
- DruidDataSource datasource = new DruidDataSource();
- datasource.setUrl(url);
- datasource.setUsername(user);
- datasource.setPassword(password);
- datasource.setDriverClassName(driverClassName);
-
- // pool configuration
- datasource.setInitialSize(initialSize);
- datasource.setMinIdle(minIdle);
- datasource.setMaxActive(maxActive);
- datasource.setMaxWait(maxWait);
- datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
- datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
- datasource.setValidationQuery(validationQuery);
- datasource.setTestWhileIdle(testWhileIdle);
- datasource.setTestOnBorrow(testOnBorrow);
- datasource.setTestOnReturn(testOnReturn);
- datasource.setPoolPreparedStatements(poolPreparedStatements);
- datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
- return datasource;
- }
-
- @Bean(name = "hiveDruidTemplate")
- public JdbcTemplate hiveDruidTemplate(@Qualifier("hiveDruidDataSource") DataSource dataSource) {
- return new JdbcTemplate(dataSource);
- }
This code is an example of configuring a Hive database connection using Spring Boot and Druid connection pool. It shows how to@ConfigurationProperties
Annotations to simplify the binding of configuration properties and how to use Spring@Bean
Annotations to create and manageDataSource
andJdbcTemplate
Bean. The following is a detailed explanation of this code:
@ConfigurationProperties(prefix = "hive")
application.properties
orapplication.yml
) is prefixed withhive
The properties are bound to the current class (HiveDruidConfig
) fields. This makes it very convenient and centralized to configure database connections and other related parameters.application.properties
There arehive.url=jdbc:hive2://...
,Sourl
The fields will be automatically assigned values.@Data
dataSource()
method@Bean
annotation, indicating that it will return a Bean, which will be managed by the Spring container. In this example, it creates and configures aDruidDataSource
Instance, which is the implementation of Druid connection pool.@ConfigurationProperties
Obtained from the configuration file.DruidDataSource
Instance, the Spring container will register it as ahiveDruidDataSource
Beans.hiveDruidTemplate()
method@Bean
Note that it creates aJdbcTemplate
Instance, this instance is used to simplify database operations.DataSource
Type parameters, through@Qualifier("hiveDruidDataSource")
The annotation specifies the use ofhiveDruidDataSource
DataSource Bean.JdbcTemplate
Instance, the Spring container will register it as ahiveDruidTemplate
Beans.This code shows how to use@ConfigurationProperties
and@Bean
Annotations are used to configure and manage database connections. By using the Druid connection pool, the performance and stability of database operations can be further improved.JdbcTemplate
, developers can perform database operations more conveniently without writing a lot of JDBC code.