一、对应的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.smilexl</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_mybatis</name>
<description>springboot整合mybatis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、相应的yml配置文件
1、application.yml配置文件
server:
port: 80
spring:
profiles:
active: dev #指定对应的配置文件
2、application-dev.yml配置文件
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
#durid数据源属性
# 数据源其他配置
initialSize: 5 #初始化连接个数
minIdle: 5 #最小连接池数量
maxActive: 20 #最大连接数量
maxWait: 60000 #获取连接时最大等待时间,单位毫秒
timeBetweenEvictionRunsMillis: 60000 #(1)Destroy线程会检测连接的间隔时间,(2)testWhileIdle的判断依据
minEvictableIdleTimeMillis: 300000 #连接保持空闲而不被驱逐的最小时间
validationQuery: SELECT 1 FROM DUAL #检测连接是否有效的sql,要求是一个查询语句
testWhileIdle: true #申请连接的时候检测
testOnBorrow: false #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
poolPreparedStatements: true #是否缓存preparedStatement
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
三、在 config 包下配置druid数据源,数据监控(DruiudConfig.java)
package cn.smilexl.springboot_mybatis.config;
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;
/**
* @ClassName: Durid数据源属性配置
* @Description: //TODO
* @Author: LL
* @Date: 2019/7/17 10:09
* @Version: V1.0
*/
@Configuration
public class DruidConfig{
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource() {
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.15.21");//拒绝谁访问
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:8081/druid