SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合

时间:2022-12-05 20:06:56

SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合

一、先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid需要的依赖

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.kyplatform</groupId>
<artifactId>kyplatform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>kyplatform</name>
<description>kyplatform for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>compile</scope>-->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<!--启动热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- druid-spring-boot-starter -->
<!-- <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

二、编写application.yml文件

# DataSource Config
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xxxxxx?characterEncoding=utf8
username: root
password: *******
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的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#filters: stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
# web-stat-filter:
# enabled: false
# jsp-servlet:
# class-name: com.alibaba.druid.support.http.StatViewServlet
# init-parameters:
# loginUsername: druid
# loginPassword: druid
# Logger Config
logging:
level:
com.baomidou.mybatisplus.samples.quickstart: debug mybatis-plus:
global-config:
db-config:
db-type: MYSQL
capital-mode: false #开启大写命名
column-like: true #开启 LIKE 查询

三、接下来编写DruidConfig、DruidStatFilter、DruidStatViewServlet三个类

DruidConfig.java

package com.kyplatform.admin.config.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;
/**
* 配置druid需要的配置类,引入application.properties文件中以spring.datasource开头的信息
* 因此需要在application.properties文件中配置相关信息。
* @author zhicaili
*
*/ @Configuration//标识该类被纳入spring容器中实例化并管理
@ServletComponentScan //用于扫描所有的Servlet、filter、listener
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid")//加载时读取指定的配置信息,前缀为spring.datasource.druid
public DataSource druidDataSource(){
return new DruidDataSource();
}
}

DruidStatFilter.java

package com.kyplatform.admin.config.druid;

import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam; /**
* 配置druid过滤规则
*/
@WebFilter(
filterName="DruidWebStatFilter",
urlPatterns = "/*",
initParams = {
@WebInitParam(name = "exclusions",value="*.js,*.jpg,*.png,*.gif,*.ico,*.css,/druid/*")//配置本过滤器放行的请求后缀
}
)
public class DruidStatFilter extends WebStatFilter { }

DruidStatViewServlet.java

package com.kyplatform.admin.config.druid;

import com.alibaba.druid.support.http.StatViewServlet;

import javax.servlet.Servlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet; /**
* 配置druid页面配置
*/
@WebServlet(urlPatterns="/druid/*",initParams = {
@WebInitParam(name="allow",value = "127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
@WebInitParam(name = "loginUsername",value = "root"),//用户名
@WebInitParam(name = "loginPassword",value = "admin"),//密码
@WebInitParam(name = "resetEnable",value = "true")// 允许HTML页面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet implements Servlet { }

配置好后,还要在springboot的启动器类中添加@ServletComponentScan

package com.kyplatform;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration; @SpringBootApplication
@ServletComponentScan
@MapperScan("com.kyplatform.admin.mapper")
public class KyplatformApplication { public static void main(String[] args) {
SpringApplication.run(KyplatformApplication.class, args);
}
}

配置好后,在浏览器中输入http://127.0.0.1:9999/项目名/druid/login.html

SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合