spring boot mysql 打印执行的sql

it2024-12-10  13

>>>>>

mysql version 8.0 以上 package cn.zhangfusheng.base.server.core.datasource.log; import lombok.extern.slf4j.Slf4j; import com.mysql.cj.MysqlConnection; import com.mysql.cj.Query; import com.mysql.cj.interceptors.QueryInterceptor; import com.mysql.cj.log.Log; import com.mysql.cj.protocol.Resultset; import com.mysql.cj.protocol.ServerSession; import java.util.Properties; import java.util.function.Supplier; /** * @author fusheng.zhang * @date 2020-10-21 14:51:40 */ @Slf4j public class AbstractQueryInterceptor implements QueryInterceptor { @Override public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) { return this; } @Override public <T extends Resultset> T preProcess(Supplier<String> sql, Query interceptedQuery) { return null; } @Override public boolean executeTopLevelOnly() { return false; } @Override public void destroy() { } @Override public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) { if (interceptedQuery != null) { log.debug("\n\turl: {}\n\tsql: {}", interceptedQuery.getSession().getHostInfo().getDatabaseUrl(), sql); } return originalResultSet; } }
最新回复(0)