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 分钟前
程序员要在你能挣钱的时候拼命存钱!
后端·面试·程序员
夜阳朔1 小时前
Conda环境激活失效问题
人工智能·后端·python
白仑色1 小时前
Spring Boot 多环境配置详解
java·spring boot·后端·微服务架构·配置管理
懒斌1 小时前
linux驱动程序
后端
超级小忍1 小时前
在 Spring Boot 中优化长轮询(Long Polling)连接频繁建立销毁问题
java·spring boot·后端
David爱编程1 小时前
Java 中 Integer 为什么不是万能的 int 替代品?
java·后端
阿宝想会飞1 小时前
easyExcel多出大量数据方法
后端
自由的疯1 小时前
基于 Java POI 实现动态列 Excel 导出的通用方法
后端
自由的疯1 小时前
Java 利用 Apache POI 实现多模板 Word 文档生成(补充:模板文档为复杂表单的处理办法)
后端
平平无奇的开发仔1 小时前
# Java 序列化与 Jackson 序列化机制对比
后端