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
相关推荐
卷福同学1 小时前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
江湖十年2 小时前
Go 并发控制:sync.Pool 详解
后端·面试·go
xdl25993 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
zb200641203 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
jwn9993 小时前
Spring Boot 整合 Keycloak
java·spring boot·后端
mldlds3 小时前
SpringBoot详解
java·spring boot·后端
kang_jin3 小时前
Spring Boot 自动配置
java·spring boot·后端
yuweiade4 小时前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
小箌4 小时前
springboot_03
spring boot·后端·状态模式
冬奇Lab4 小时前
一天一个开源项目(第54篇):Supabase - 开源的 Postgres 开发平台,Firebase 替代方案
后端·开源·资讯