SpringBoot之JdbcTemplate输出完整SQL日志

applicatio.yml开启日志功能

java 复制代码
jdbc-log:
  # 开启完整SQL日志输出功能
  enabled: true

logging:
  level:
  	# 切面类路径,日志级别为DEBUG,因为SpringBoot默认日志级别为INFO
    com.xxx.xxx.JdbcTemplateAspect: DEBUG

日志切面

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

@Slf4j
@Aspect
@Component
@ConditionalOnProperty(prefix = "jdbc-log", value = "enabled", havingValue = "true")
public class JdbcTemplateAspect {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	
	//执行切面前,输出完整SQL,防止执行SQL过程中报错,导致无法输出完整SQL。
    @Before(value = "execution(* org.springframework.jdbc.core.JdbcTemplate.*(..))")
    public void afterQuery(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (ArrayUtils.isNotEmpty(args) && args[0] instanceof String && args[2] instanceof Object[]) {
            String sql = ((String) args[0]).replaceAll("[\\s\\n\\t]+", " ");
            Object[] params = (Object[]) args[2];
            for (Object propertyValue : params) {
                String value;
                if (Objects.isNull(propertyValue)) {
                    value = "null";
                    sql = sql.replaceFirst("\\?", value);
                    continue;
                }
                if (propertyValue instanceof String) {
                    value = "'" + propertyValue + "'";
                } else if (propertyValue instanceof Date) {
                    value = "'" + DATE_TIME_FORMATTER.format(((Date) propertyValue).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()) + "'";
                } else if (propertyValue instanceof LocalDate) {
                    value = "'" + DATE_FORMATTER.format((LocalDate) propertyValue) + "'";
                } else if (propertyValue instanceof LocalDateTime) {
                    value = "'" + DATE_TIME_FORMATTER.format((LocalDateTime) propertyValue) + "'";
                } else {
                    value = propertyValue.toString();
                }
                sql = sql.replaceFirst("\\?", value);
            }
            log.debug("\033[32mexecute SQL:\n{}\033[0m", sql);
        }
    }
}

启动项目,调用某个JdbcTemplate类里面的方法就会输出完整SQL到控制台,如下内容

sql 复制代码
execute SQL:
select * from table where id=1
相关推荐
han_hanker32 分钟前
SQL语法 一个统计的查询
sql
乐橙开放平台43 分钟前
一周上线校园透明化:listDeviceDetailsByPage 台账 + getKitToken + ImouPlayer 多路墙
后端·物联网·安全·音视频·智能家居
樊小肆1 小时前
# 你还在等DeepSeek官方 agent Harness‌? 来试试 DeepSeeker-Code吧
前端·人工智能·后端
樊小肆1 小时前
2568 万 token 才花 2 块 2:聊聊 DeepSeeker-Code 怎么吃满上下文缓存
前端·人工智能·后端
众人皆醒我独醉1 小时前
大模型训练优化:FSDP、DeepSpeed ZeRO 与混合精度
后端·面试·gpu
Zane19941 小时前
ClassName() 只是一步?拆开看 __new__ 和 __init__ 各自在干什么
后端·python
geovindu1 小时前
java: Memento Pattern
java·开发语言·后端·备忘录模式·行为模式
星哥的编程之路1 小时前
万字深度解析 Agent 学习路线
后端
程序边界1 小时前
SQL Server数据迁移这件事,远比你想的复杂——但也远比你想的简单(下)
后端
Zane19941 小时前
JRE 去哪了?一次讲清楚 JDK、JRE、JVM 到底是什么关系?
java·后端