Springboot自定义starter打印sql及其执行时间


前面写到了通过实现mybatis提供的org.apache.ibatis.plugin.Interceptor接口实现了打印SQL执行时间,并格式化SQL及其参数,如果我们使用的是ssm还得再配置文件中添加一小段配置,如果使用的是Springboot,也得把bean注入到spring的IOC容器中。哎,谁让我们是一个懒人呢?既然是一个懒人,就把懒人做到极致,嘻嘻嘻,今天我们就通过自定义Springboot starter的方式使用该插件,我们使用的时候只需要把对应的依赖添加到我们项目的pom.xml中,其他的就什么也不用做了。

先看一下项目结构:

 pom.xml文件的内容如下:

主要依赖了Springboot中自动配置的一些依赖,具体请参考:

https://docs.spring.io/spring-boot/docs/2.4.1/reference/html/appendix-configuration-metadata.html#configuration-metadata-format

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.liekkasgroupId>
    <artifactId>sql-cost-spring-boot-starterartifactId>
    <version>1.0.0version>
    <name>sql-cost-printname>
    <description>打印SQL执行时间和SQL语句description>

    <properties>
        <java.version>11java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
        dependency>
    dependencies>

    <build>
        <finalName>
            sql-cost-spring-boot-starter
        finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <skip>
                        true
                    skip>
                configuration>
            plugin>
        plugins>
    build>

project>

自定义打印SQL执行时间starter的过程如下:

第一步:定义配置类,用来接收application.yml中的参数

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * description
 *
 * @author liekkas 2021/01/09 17:12
 */
@ConfigurationProperties(prefix = "sql-cost-print")
public class SqlPluginProperties {

    private boolean enable;

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}

@ConfigurationProperties的作用是将配置文件转换成类对象,获取application.yml配置的值

其中prefix = "sql-cost-print"的作用是在application.yml配置的增加一个前缀。

 

第二步:自动装配打印SQL的Bean

import com.liekkas.sqlcost.plugin.SqlCostPlugins;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description
 *
 * @author liekkas 2021/01/09 16:32
 */
@Configuration
@EnableConfigurationProperties(SqlPluginProperties.class)
@ConditionalOnProperty(prefix = "sql-cost-print", name = "enable",
        havingValue = "true", matchIfMissing = true)
public class AutoConfigSqlCostPlugin {
    @Bean
    @ConditionalOnClass(value = StatementHandler.class)
    public SqlCostPlugins sqlCostPlugins() {
        return new SqlCostPlugins();
    }
}

 

@Configuration注解的作用是标记该类是一个配置类

@EnableConfigurationProperties的作用是开启@ConfigurationProperties,使SqlPluginProperties中的属性生效

@ConditionalOnProperty来控制自动配置是否生效,havingValue = "true"代表当sql-cost-print.enable=true的时候自动配置才会生效,matchIfMissing = true代表配置文件中没有配置时也会生效。

@ConditionalOnClass(value = StatementHandler.class)的作用是只有再类路径中有StatementHandler.class的时候才会将SqlCostPlugins注入到IOC容器中。

综上,按照本starter的自动配置方式,只有当application.yml中的sql-cost-print.enable=true或者不配置时打印SQL,其他都不会打印SQL。

第三步:在spring.factories中添加配置

完整的路径为src/main/resources/META-INF/spring.factories

添加的内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.liekkas.sqlcost.configuration.AutoConfigSqlCostPlugin

最后使用mvn clean install 打成jar包放入自己本地的maven仓库,使用的时候只需在自己的项目中添加下面的依赖即可,仅限Springboot的项目使用哦!

<dependency>
    <groupId>com.liekkasgroupId>
    <artifactId>sql-cost-spring-boot-starterartifactId>
    <version>1.0.0version>
dependency>

当不想启用打印SQL的时候只需要在application.yml中添加以下配置即可。

sql-cost-print:
enable: false

 

上边代码中的SqlCostPlugins类的具体内容请参考我的另一篇博客:

https://www.cnblogs.com/wkynf/p/14102201.html