当前位置:   article > 正文

Mybatis sql 控制台格式化

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<String> 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<String> NEED_BRACKETS;
  57. static {
  58. Set<String> 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<Map.Entry<String, String>> 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<String, String> 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<Map.Entry<String, String>> parseParams(String line) {
  89. line = StringUtils.removeEnd(line, "\n");
  90. final String[] strings = StringUtils.splitByWholeSeparator(line, ", ");
  91. final Queue<Map.Entry<String, String>> 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

运行后,结果

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号