解决问题:
SpringBoot2.0+MyBatis框架集成阿里Druid连接池?
本章基于SpringBoot专栏(三) -- SpingBoot集成MyBatis框架的代码。
1.在pom.xml文件中导入Jar包。
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mungerz</groupId> <artifactId>FirstSpringBootDemo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> </dependencies> </project>
2.修改SpringBoot配置文件,application.xml
spring:
profiles:
active: dev
#配置数据源
datasource:
url: jdbc:mysql://192.168.43.167:3306/mungerTest01
username: root
password: Munger_123
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 300
minIdle: 100
maxActive: 1000
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
#exceptionSorter: true
testOnReturn: false
poolPreparedStatements: true
#filter: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
useGlobalDataSourceStat: true
mybatis:
typeAliasesPackage: com.mungerz.dao
mapperLocations: classpath:mapper/*.xml
关键配置:
#Druid连接池,初始化时创建的连接数
initialSize: 300 Druid连接池
#Druid连接池,空闲时,连接池中保留的最小连接数
minIdle: 100
#Druid连接池,最大活跃连接数
maxActive: 1000
(配置信息参考官网:https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE)
3.SpringBoot注入Druid连接池
创建如下文件:
DruidStatFilter.java
package com.mungerz.filter; import com.alibaba.druid.support.http.WebStatFilter; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @WebFilter(filterName="druidWebStatFilter", urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"),// 忽略资源 }) public class DruidStatFilter extends WebStatFilter { }
DruidStatViewServlet.java
package com.mungerz.filter; import com.alibaba.druid.support.http.StatViewServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; @WebServlet(urlPatterns = "/druid/*", initParams={ @WebInitParam(name="allow",value=""),// IP白名单 (没有配置或者为空,则允许所有访问) @WebInitParam(name="deny",value="192.168.16.11"),// IP黑名单 (存在共同时,deny优先于allow) @WebInitParam(name="loginUsername",value="admin"),// 用户名 @WebInitParam(name="loginPassword",value="admin"),// 密码 @WebInitParam(name="resetEnable",value="true")// 禁用HTML页面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = 2359758657306626394L; }
Application.java
package com.mungerz; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; // SpringBootApplication注解:负责启动SpringBoot项目。这个注解实现了扫描包的功能,扫描范围是该文件所在的当前目录和同级目录。 // 我们需要格外注意这个注解所在文件的位置,因为它和扫包有关系。什么是扫包?就是发现Service,Controller,Component然后自动注入到Spring容器中。 @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } @Bean("druidDataSource") @ConfigurationProperties(prefix="spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); } }
这样,Druid连接池就集成成功了。直接运行项目,会找到如下日志:
4.登录Druid监控界面
项目运行成功以后,在浏览器地址输入:http://localhost:8080/druid/login.html
(转载请标明出处)