SpringBoot使用Druid快速入门
在SpringBoot中设置Druid配置类,加载到容器当中
一、导入依赖
<!--阿里连接池druid:1.1.14-2019年--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.14</version> </dependency>
二、properties配置类
# 数据库驱动: spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 数据源名称 spring.datasource.name=defaultDataSource # 数据库连接地址 spring.datasource.url=jdbc:mysql://localhost:3306/lingling_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai # 数据库用户名&密码: spring.datasource.username=admin spring.datasource.password=admin
三、Druid配置,可根据自身调整
package com.llkj.common.config; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; /** * @author liuzhihao * @version 1.0.0 * @ClassName DruidConfig * @Description Druid监控web配置 * @date 2021/8/19 16:23 * @copyright 零瓴软件 */ @Configuration public class DruidConfig { @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); // 登录URL http://localhost:8080/d/login.html reg.addUrlMappings("/druid/*"); // 设置白名单 reg.addInitParameter("allow", "192.168.1.7"); // 设置黑名单 reg.addInitParameter("deny", ""); // 设置登录查看信息的账号密码. reg.addInitParameter("loginUsername", "admin"); reg.addInitParameter("loginPassword", "admin"); return reg; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } @Bean public DataSource druidDataSource( @Value("${spring.datasource.driverClassName}") String driver, @Value("${spring.datasource.url}") String url, @Value("${spring.datasource.username}") String username, @Value("${spring.datasource.password}") String password, @Value("${publicKey}") String publicKey, @Value("${spring.datasource.initialSize}") int initialSize, @Value("${spring.datasource.minIdle}") int minIdle, @Value("${spring.datasource.maxActive}") int maxActive, @Value("${spring.datasource.maxWait}") long maxWait, @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis, @Value("${spring.datasource.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis, @Value("${spring.datasource.validationQuery}") String validationQuery, @Value("${spring.datasource.testWhileIdle}") boolean testWhileIdle, @Value("${spring.datasource.testOnBorrow}") boolean testOnBorrow, @Value("${spring.datasource.testOnReturn}") boolean testOnReturn, @Value("${spring.datasource.poolPreparedStatements}") boolean poolPreparedStatements, @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") int maxPoolPreparedStatementPerConnectionSize, @Value("${spring.datasource.filters}") String filters, @Value("${spring.datasource.connectionProperties}") String connectionProperties) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setInitialSize(initialSize); druidDataSource.setMaxActive(maxActive); druidDataSource.setMaxWait(maxWait); druidDataSource .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); druidDataSource .setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); druidDataSource.setValidationQuery(validationQuery); druidDataSource.setTestWhileIdle(testWhileIdle); druidDataSource.setTestOnBorrow(testOnBorrow); druidDataSource.setTestOnReturn(testOnReturn); druidDataSource.setPoolPreparedStatements(poolPreparedStatements); druidDataSource .setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); druidDataSource.setConnectionProperties(connectionProperties); try { druidDataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } return druidDataSource; } }
四、访问druid的ui地址
http://localhost:8080/druid/login.html
用户名:admin
密码:admin