如何生成SQL语句

场景:根据业务逻辑 动态生成 sql语句各部分,比如select部分、where部分等,然后最终生成完整的sql

工具:org.apache.ibatis.jdbc.SqlBuilder(mybatis包内含)

  1. /**
  2. * Copyright 2009-2018 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.ibatis.jdbc;
  17. /**
  18. * @deprecated Use the {@link SQL} Class
  19. *
  20. * @author Jeff Butler
  21. */
  22. public class SqlBuilder {
  23. private static final ThreadLocal<SQL> localSQL = new ThreadLocal<>();
  24. static {
  25. BEGIN();
  26. }
  27. private SqlBuilder() {
  28. // Prevent Instantiation
  29. }
  30. public static void BEGIN() {
  31. RESET();
  32. }
  33. public static void RESET() {
  34. localSQL.set(new SQL());
  35. }
  36. public static void UPDATE(String table) {
  37. sql().UPDATE(table);
  38. }
  39. public static void SET(String sets) {
  40. sql().SET(sets);
  41. }
  42. public static String SQL() {
  43. try {
  44. return sql().toString();
  45. } finally {
  46. RESET();
  47. }
  48. }
  49. public static void INSERT_INTO(String tableName) {
  50. sql().INSERT_INTO(tableName);
  51. }
  52. public static void VALUES(String columns, String values) {
  53. sql().VALUES(columns, values);
  54. }
  55. public static void SELECT(String columns) {
  56. sql().SELECT(columns);
  57. }
  58. public static void SELECT_DISTINCT(String columns) {
  59. sql().SELECT_DISTINCT(columns);
  60. }
  61. public static void DELETE_FROM(String table) {
  62. sql().DELETE_FROM(table);
  63. }
  64. public static void FROM(String table) {
  65. sql().FROM(table);
  66. }
  67. public static void JOIN(String join) {
  68. sql().JOIN(join);
  69. }
  70. public static void INNER_JOIN(String join) {
  71. sql().INNER_JOIN(join);
  72. }
  73. public static void LEFT_OUTER_JOIN(String join) {
  74. sql().LEFT_OUTER_JOIN(join);
  75. }
  76. public static void RIGHT_OUTER_JOIN(String join) {
  77. sql().RIGHT_OUTER_JOIN(join);
  78. }
  79. public static void OUTER_JOIN(String join) {
  80. sql().OUTER_JOIN(join);
  81. }
  82. public static void WHERE(String conditions) {
  83. sql().WHERE(conditions);
  84. }
  85. public static void OR() {
  86. sql().OR();
  87. }
  88. public static void AND() {
  89. sql().AND();
  90. }
  91. public static void GROUP_BY(String columns) {
  92. sql().GROUP_BY(columns);
  93. }
  94. public static void HAVING(String conditions) {
  95. sql().HAVING(conditions);
  96. }
  97. public static void ORDER_BY(String columns) {
  98. sql().ORDER_BY(columns);
  99. }
  100. private static SQL sql() {
  101. return localSQL.get();
  102. }
  103. }