一、pom.xml配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
二、application.properties配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.name=druidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/billdelivery?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=seeyon123456
# 最大连接数
spring.datasource.druid.max-active=20
# 初始连接数
spring.datasource.druid.initial-size=5
# 最小连接数
spring.datasource.druid.min-idle=5
# 获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
# 测试连接
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# 申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性
spring.datasource.druid.test-while-idle=true
# 获取连接时执行检测,建议关闭,影响性能
spring.datasource.druid.test-on-borrow=false
# 归还连接时执行检测,建议关闭,影响性能
spring.datasource.druid.test-on-return=false
# 是否缓存preparedStatement 在mysql的环境下建议关闭 因为对数据库性能消耗大
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
# 配置监控统计拦截的filters,去掉后监控界面SQL无法进行统计,'wall'用于防火墙
spring.datasource.druid.filters=stat,wall,slf4j
# 打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# IP白名单
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
# IP黑名单
#spring.datasource.druid.stat-view-servlet.deny=192.168.1.73
# 禁用HTML页面上的“Reset All”功能
spring.datasource.druid.stat-view-servlet.reset-enable=true
# 登陆名
spring.datasource.druid.stat-view-servlet.login-username=admin
# 登录密码
spring.datasource.druid.stat-view-servlet.login-password=123456
三、测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class DruidApplicationTests {
@Autowired
private DataSource dataSource;
@Test
public void contextLoads() throws Exception {
Connection connection = dataSource.getConnection();
PreparedStatement prepareStatement = connection.prepareStatement("select * from bill_item");
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
String cityName = resultSet.getString("id");
System.out.println(cityName);
}
}
}
执行结果:
-2592516606220286509
511882148076407261
2223847085336053894
7407386164171327186
四、druid获得SQL执行日志
重点关注application.properties中以下片段:
# 打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# IP白名单
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
# IP黑名单
#spring.datasource.druid.stat-view-servlet.deny=192.168.1.73
# 禁用HTML页面上的“Reset All”功能
spring.datasource.druid.stat-view-servlet.reset-enable=true
# 登陆名
spring.datasource.druid.stat-view-servlet.login-username=admin
# 登录密码
spring.datasource.druid.stat-view-servlet.login-password=123456
我们做一个基于mybatis的查询例子:
@Repository
public interface BillShopMapper extends BaseMapper<BillShop> {
}
@RestController
public class HelloController {
@Autowired
private BillShopMapper billShopMapper;
@RequestMapping(path = "/listAll", method = RequestMethod.GET)
public List<BillShop> listAll() {
return billShopMapper.selectList(null);
}
}
访问/listAll请求,从而触发sql查询:
注意:不要轻易升级druid-spring-boot-starter,经验证升级到最新1.1.20就会访问不了。
五、参考链接
- 【SpringBoot笔记】SpringBoot整合Druid数据连接池
- Druid(新版starter)在SpringBoot下的使用
- springboot2.0整合druid,以及springboot自动装配DataSource原理
- spring-boot整合阿里druid