One. Overview
We first configure a data source among the Springboot.
Two. Configuration of the data source
In Springboot, it is very easy to configure a data source. We just need to use the Application.properties file.
spring.datasource.password=trekspring.datasource.username=rootspring.datasource.url=jdbc:mysql:// Localhost:3306/springbootspring.datasource.driver-class-name=com.mysql.jdbc.driver
As long as we have configured the above content in the configuration file, and we have introduced the relevant initiators, our data source will be automatically configured.
The initiators are as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-jdbc</artifactid> </dependency> <dependency> <groupId> mysql</groupid> <artifactId>mysql-connector-java</artifactId> <scope>runtime </scope> </dependency>
We are using the MySQL database.
Three. Configuring the Druid Data source
In general, we will not use the Springboot default data source, in general we will use Druid to complete.
The best thing about Druid is having a complete set of monitoring components.
First we need to import the Druid jar file.
<dependency> <groupId>com.alibaba</groupId> <artifactid>druid</artifactid > <version>1.1.0</version> </dependency>
The version we used this time is 1.1.0 this version, because I like to use an integer-like version. hehe.
The next thing we need is a druid configuration.
Spring.datasource.type=com.alibaba.druid.pool.druiddatasource
We only need to specify in the configuration information above that the type we need to use is druid.
Now there is a problem, that is, there is a lot of configuration information in the Druid data source, what means can we use to configure these properties?
Spring.datasource.initialsize=5 spring.datasource.minidle=5 spring.datasource.maxactive=20 spring.datasource.maxwait=60000 spring.datasource.timebetweenevictionrunsmillis=60000 spring.datasource.minevictableidletimemillis=300000 spring.datasource.validationquery=select 1 from DUAL Spring.datasource.testwhileidle=true Spring.datasource.testonborrow=false Spring.datasource.testonreturn=false spring.datasource.poolpreparedstatements=true Spring.datasource.maxpoolpreparedstatementperconnectionsize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000
We also configure the above information in the Application.properties file:
Careful people will find that these configuration information is not part of the Springboot standard configuration information at all.
Below we can use an annotation to help us complete [email protected] to help us.
Here's a simple demo:
@Configurationpublic class Datasourceconfig { @Bean @ConfigurationProperties ("Spring.datasource") Public DataSource Druid () { return new Druiddatasource ();} }
We have added a note to the general bean configuration that will help us automatically populate some of the configuration information in the configuration file on the Bean's properties.
012 Druid Data Source