Springboot in the default is the use of Tomcat connection pool, if we want to third-party connection pool, so we configure it?
First, in the Application.yml file, comment out the configuration of the previous database, re-configured in Druid mode:
#spring: #datasource: # driver-class-Name:com.mysql.jdbc.Driver # Url:jdbc:mysql://127.0.0.1:3306/house#username: Root #password: Rootmybatis.config-location = classpath:/mybatis/mybatis-CONFIG. spring.druid.url= Jdbc:mysql://127.0.0.1:3306/houseSpring.druid.username =Root Spring.druid.password=Root Spring.druid.driverClassName=Com.mysql.jdbc.Driver spring.druid.initialSize=5Spring.druid.minIdle=5spring.druid.maxActive= -spring.druid.maxWait=10000Spring.druid.timeBetweenEvictionRunsMillis=60000Spring.druid.minEvictableIdleTimeMillis=300000Spring.druid.validationQuery=select1From DUALspring.druid.testWhileIdle=true
To configure the dependent libraries in the Pom file:
<dependency> <groupId>com.alibaba</groupId> <artifactid>druid</artifactid > <version>1.1. 8</version> </dependency> <dependency> <groupid>com.google.guava</ groupid> <artifactId>guava</artifactId> <version>18.0</version > <!--Pay special attention to this place version is also not messy--- </dependency>
To create a new druidconfig configuration file:
@Configuration Public classdruidconfig {@ConfigurationProperties (prefix="Spring.druid")//configuring spring.druid.* in configuration files to Druiddatasource@Bean (initmethod="Init", destroymethod="Close") Publicdruiddatasource DataSource (Filter statfilter) throws sqlexception{Druiddatasource DataSource=NewDruiddatasource (); Datasource.setproxyfilters (Lists.newarraylist (Statfilter ())); returnDataSource; } //Configure slow Connection filtering@Bean PublicFilter Statfilter () {statfilter filter=NewStatfilter (); Filter.setslowsqlmillis ( the); Filter.setlogslowsql (true); Filter.setmergesql (true); returnfilter; } //adding monitoring can help us analyze slow SQL and execution time for each SQL@Bean PublicServletregistrationbean Servletregistrationbean () {return NewServletregistrationbean (NewStatviewservlet (),"/druid/*"); } }
Then start the project, enter the address in the browser http://127.0.0.1:8080/user the user table in the database query, and then access Http://127.0.0.1:8080/druid will display the page as follows, you can the data source, SQL monitoring, etc. to query:
springboot--data-tier access to build an integrated DUID connection pool