1.pom
<!--数据库里连接池druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.application.yml
server:
port: 8889
#mysql连接参数
spring:
application:
name: shenning-mysql
datasource:
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包(com.mysql.cj.jdbc.Driver 6,org.gjt.mm.mysql.Driver)
# 数据库名称(serverTimezone时区的问题)
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: shenning
type: com.alibaba.druid.pool.DruidDataSource
#---------------------------------------
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
#---------------------------------------
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开 PSCache,并且指定每个连接上 PSCache 的大小
#oracle设为true,mysql设为false。分库分表较多推荐设置为false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的 Filter,去掉后监控界面 SQL 无法统计,wall 用于防火墙
#如果运行时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可
filters: stat,wall,log4j
#合并多个DruidDataSource的监控数据
#useGlobalDataSourceStat: true
# 通过 connection-properties 属性打开 mergeSql 功能;慢 SQL 记录
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
#mybatis配置
mybatis:
configuration:
call-setters-on-nulls: true #如果查询语句中某些字段值是null的,则这个字段就无法返回
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql
mapper-locations: classpath:mapper/*.xml,classpath:mappers/*.xml #Mapper.xml所在的位置
3.配置工具
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @program: springboot-demp
* @description:
* @author: shenning
* @create: 2020-01-16 15:14
*/
@Configuration
public class DruidConfig {
/**
* 创建一个数据源,并且绑定属性
* @return
*/
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
// 配置Druid的监控
// 1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
//账号密码
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "123456");
//默认就是允许所有访问
initParams.put("allow", "");
//拒绝谁来访问
initParams.put("deny", "192.168.6.11");
bean.setInitParameters(initParams);
return bean;
}
// 2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<>();
//不拦截哪些请求
initParams.put("exclusions", "*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
//拦截所有请求
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
http://localhost:8080/druid/login.html 访问admin/123456