• Mybatis sql 控制台格式化


    1. package com.mysql;
    2. import org.apache.commons.lang.StringUtils;
    3. import org.apache.ibatis.logging.Log;
    4. import java.util.*;
    5. /**
    6. * @Description: sql 格式化
    7. * @Author: DingQiMing
    8. * @Date: 2023-07-17
    9. * @Version: V1.0
    10. */
    11. public class StdOutImpl implements Log {
    12. private static final String PREPARING_KEY = "==> Preparing: ";
    13. private static final String PARAMETERS_KEY = "==> Parameters: ";
    14. ThreadLocal threadLocal = new ThreadLocal<>();
    15. public StdOutImpl(String clazz) {
    16. // Do Nothing
    17. }
    18. @Override
    19. public boolean isDebugEnabled() {
    20. return true;
    21. }
    22. @Override
    23. public boolean isTraceEnabled() {
    24. return true;
    25. }
    26. @Override
    27. public void error(String s, Throwable e) {
    28. System.err.println(s);
    29. e.printStackTrace(System.err);
    30. }
    31. @Override
    32. public void error(String s) {
    33. System.err.println(s);
    34. }
    35. @Override
    36. public void debug(String s) {
    37. System.out.println(s);
    38. if (s.startsWith(PREPARING_KEY)) {
    39. threadLocal.set(s.replaceAll(PREPARING_KEY, ""));
    40. }else if (s.startsWith(PARAMETERS_KEY)) {
    41. String sql = threadLocal.get();
    42. String params = s.replaceAll(PARAMETERS_KEY, "");
    43. String log = this.parseSql(sql,parseParams(params)).toString();
    44. System.err.println("==> Log: " + log);
    45. }
    46. }
    47. @Override
    48. public void trace(String s) {
    49. System.out.println(s);
    50. }
    51. @Override
    52. public void warn(String s) {
    53. System.out.println(s);
    54. }
    55. private static final char MARK = '?';
    56. private static final Set NEED_BRACKETS;
    57. static {
    58. Set types = new HashSet<>(8);
    59. types.add("String");
    60. types.add("Date");
    61. types.add("Time");
    62. types.add("LocalDate");
    63. types.add("LocalTime");
    64. types.add("LocalDateTime");
    65. types.add("BigDecimal");
    66. types.add("Timestamp");
    67. NEED_BRACKETS = Collections.unmodifiableSet(types);
    68. }
    69. static StringBuilder parseSql(String sql, Queue> params) {
    70. final StringBuilder sb = new StringBuilder(sql);
    71. for (int i = 0; i < sb.length(); i++) {
    72. if (sb.charAt(i) != MARK) {
    73. continue;
    74. }
    75. final Map.Entry entry = params.poll();
    76. if (Objects.isNull(entry)) {
    77. continue;
    78. }
    79. sb.deleteCharAt(i);
    80. if (NEED_BRACKETS.contains(entry.getValue())) {
    81. sb.insert(i, String.format("'%s'", entry.getKey()));
    82. } else {
    83. sb.insert(i, entry.getKey());
    84. }
    85. }
    86. return sb;
    87. }
    88. static Queue> parseParams(String line) {
    89. line = StringUtils.removeEnd(line, "\n");
    90. final String[] strings = StringUtils.splitByWholeSeparator(line, ", ");
    91. final Queue> queue = new ArrayDeque<>(strings.length);
    92. for (String s : strings) {
    93. String value = StringUtils.substringBeforeLast(s, "(");
    94. String type = StringUtils.substringBetween(s, "(", ")");
    95. if (StringUtils.isEmpty(type)) {
    96. queue.offer(new AbstractMap.SimpleEntry<>(value, null));
    97. } else {
    98. queue.offer(new AbstractMap.SimpleEntry<>(value, type));
    99. }
    100. }
    101. return queue;
    102. }
    103. }

    修改application.properties文件中

    mybatis.configuration.log-impl:com.mysql.StdOutImpl

    运行后,结果

  • 相关阅读:
    RSS阅读器
    【O2O领域】Axure外卖配送代理商调度系统原型,生鲜配送站团队管理后台原型
    react native使用5-搭建ios环境
    TI/德州仪器 TPD13S523RSVR HDMI 13 通道 ESD保护解决方案
    计算机网络 ——HTTP协议(一)
    瑞芯微rk1126 平台部分jpeg图片解码程序挂掉的问题
    力扣--深度优先算法/回溯算法40.组合总和 Ⅱ
    第三章类与对象基础 ② 代码
    一文解析推特上最常见的加密骗局
    JVM(尚硅谷)学习之GC日志分析
  • 原文地址:https://blog.csdn.net/ding43930053/article/details/136652990