mybatis-plus3.5.10.x版本使用p6spy打印sql

mybatis-plus3.5.10.x版本使用p6spy打印sql

1、导入依赖

     <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
        <dependency>
            <groupId>com.github.gavlyukovskiy</groupId>
            <artifactId>p6spy-spring-boot-starter</artifactId>
            <version>1.10.0</version>
        </dependency>

2、配置数据库连接

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://172.16.211.95:3306/hntt-dev?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=hnttdev

3、配置文件  P6SPYConfig

import com.alibaba.fastjson2.JSONObject;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class P6SPYConfig  implements MessageFormattingStrategy {

    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    /**
     * 自定义sql日志打印
     *
     * @param connectionId 连接标识
     * @param now          执行时间
     * @param elapsed      执行秒数ms
     * @param category     statement
     * @param prepared     预编译sql语句
     * @param sql          真实的sql语句
     * @param url          数据库url连接
     * @return {@link String}
     */
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        Map<String, Object> message = new LinkedHashMap<>(4);
        if (StringUtils.isNoneBlank(prepared, sql) ) {
            message.put("SQL execute time:", this.format.format(new Date()));
            message.put("SQL explain time:", elapsed + "ms");
            String newPrepared = prepared.replace("   ", "").replace("\n", " ");
            message.put("SQL prepared:", newPrepared);
            String newSql = sql.replace("   ", "").replace("\n", " ");
            message.put("SQL real:", newSql);
        }
        return JSONObject.toJSONString(message.isEmpty() ? "" : message);

    }
}

其他没有了,注意 spring.datasource.url=jdbc:p6spy:mysql://xxx     要加上jdbc:p6spy 很多人就是遗漏了这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑56

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值