赞
踩
- package com.mysql;
- import org.apache.commons.lang.StringUtils;
- import org.apache.ibatis.logging.Log;
-
- import java.util.*;
-
- /**
- * @Description: sql 格式化
- * @Author: DingQiMing
- * @Date: 2023-07-17
- * @Version: V1.0
- */
- public class StdOutImpl implements Log {
- private static final String PREPARING_KEY = "==> Preparing: ";
- private static final String PARAMETERS_KEY = "==> Parameters: ";
- ThreadLocal<String> threadLocal = new ThreadLocal<>();
- public StdOutImpl(String clazz) {
- // Do Nothing
- }
-
- @Override
- public boolean isDebugEnabled() {
- return true;
- }
-
- @Override
- public boolean isTraceEnabled() {
- return true;
- }
-
- @Override
- public void error(String s, Throwable e) {
- System.err.println(s);
- e.printStackTrace(System.err);
- }
-
- @Override
- public void error(String s) {
- System.err.println(s);
- }
-
- @Override
- public void debug(String s) {
- System.out.println(s);
- if (s.startsWith(PREPARING_KEY)) {
- threadLocal.set(s.replaceAll(PREPARING_KEY, ""));
- }else if (s.startsWith(PARAMETERS_KEY)) {
- String sql = threadLocal.get();
- String params = s.replaceAll(PARAMETERS_KEY, "");
- String log = this.parseSql(sql,parseParams(params)).toString();
- System.err.println("==> Log: " + log);
- }
- }
-
- @Override
- public void trace(String s) {
- System.out.println(s);
- }
-
- @Override
- public void warn(String s) {
- System.out.println(s);
- }
-
- private static final char MARK = '?';
-
- private static final Set<String> NEED_BRACKETS;
-
- static {
- Set<String> types = new HashSet<>(8);
- types.add("String");
- types.add("Date");
- types.add("Time");
- types.add("LocalDate");
- types.add("LocalTime");
- types.add("LocalDateTime");
- types.add("BigDecimal");
- types.add("Timestamp");
- NEED_BRACKETS = Collections.unmodifiableSet(types);
- }
-
- static StringBuilder parseSql(String sql, Queue<Map.Entry<String, String>> params) {
- final StringBuilder sb = new StringBuilder(sql);
- for (int i = 0; i < sb.length(); i++) {
- if (sb.charAt(i) != MARK) {
- continue;
- }
- final Map.Entry<String, String> entry = params.poll();
- if (Objects.isNull(entry)) {
- continue;
- }
- sb.deleteCharAt(i);
- if (NEED_BRACKETS.contains(entry.getValue())) {
- sb.insert(i, String.format("'%s'", entry.getKey()));
- } else {
- sb.insert(i, entry.getKey());
- }
- }
- return sb;
- }
-
- static Queue<Map.Entry<String, String>> parseParams(String line) {
- line = StringUtils.removeEnd(line, "\n");
- final String[] strings = StringUtils.splitByWholeSeparator(line, ", ");
- final Queue<Map.Entry<String, String>> queue = new ArrayDeque<>(strings.length);
- for (String s : strings) {
- String value = StringUtils.substringBeforeLast(s, "(");
- String type = StringUtils.substringBetween(s, "(", ")");
- if (StringUtils.isEmpty(type)) {
- queue.offer(new AbstractMap.SimpleEntry<>(value, null));
- } else {
- queue.offer(new AbstractMap.SimpleEntry<>(value, type));
- }
- }
- return queue;
- }
- }
修改application.properties文件中
mybatis.configuration.log-impl:com.mysql.StdOutImpl
运行后,结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。