TableToJsonWeb.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.java110.code;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.java110.utils.util.StringUtil;
  5. public class TableToJsonWeb {
  6. //show create table c_orders 用这个语句获取
  7. public static final String createTableSql = "CREATE TABLE `fee_combo` (\n" +
  8. " `combo_id` varchar(30) NOT NULL COMMENT '套餐ID',\n" +
  9. " `combo_name` varchar(30) NOT NULL COMMENT '套餐名称',\n" +
  10. " `community_id` varchar(30) NOT NULL COMMENT '小区ID'\n" +
  11. ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  12. public static void main(String[] args) {
  13. String templateName = "费用套餐"; //业务名称
  14. String templateCode = "feeCombo"; //表名大写
  15. String templateKey = "comboId"; //表主键
  16. String templateKeyName = "编号";//主键说明
  17. String searchCode = "comboId"; //分片字段
  18. String searchName = "编号"; //分片字段说明
  19. String directories = "property"; //前端生成到那个目录下
  20. // templateName 业务名称 业务编码名称生成后文件名 templateCode 主键 templateKey
  21. // 业务主键名称 templateKeyName=templateName+ID 主机驼峰 searchCode 主键名称 searchName
  22. // directories 放在前端那个目录下
  23. String newSql = createTableSql.substring(createTableSql.indexOf("(") + 1, createTableSql.lastIndexOf(")"));
  24. String tableName = createTableSql.substring(createTableSql.indexOf("TABLE") + 5, createTableSql.indexOf("("));
  25. tableName = tableName.replaceAll("`", "").trim();
  26. newSql = newSql.replaceAll("\n", "");
  27. String[] rowSqls = newSql.split("',");
  28. JSONObject param = new JSONObject();
  29. param.put("templateName", templateName);
  30. param.put("templateCode", templateCode);
  31. param.put("templateKey", templateKey);
  32. param.put("templateKeyName", templateKeyName);
  33. param.put("searchCode", searchCode);
  34. param.put("searchName", searchName);
  35. param.put("directories", directories);
  36. JSONObject paramColumn = null;
  37. JSONArray conditions = new JSONArray();
  38. JSONArray paramColumns = new JSONArray();
  39. JSONObject condition = null;
  40. String key = "";
  41. for (String rowSql : rowSqls) {
  42. condition = new JSONObject();
  43. paramColumn = new JSONObject();
  44. key = rowSql.trim();
  45. key = key.substring(0, key.indexOf(" "));
  46. key = key.replaceAll("`", "");
  47. if ("UNIQUE".equals(key)) {
  48. continue;
  49. }
  50. if ("KEY".equals(key)) {
  51. continue;
  52. }
  53. if ("b_id".equals(key)) {
  54. continue;
  55. }
  56. if ("create_time".equals(key)) {
  57. continue;
  58. }
  59. if ("status_cd".equals(key)) {
  60. continue;
  61. }
  62. String comment = rowSql.contains("COMMENT") ? rowSql.substring(rowSql.indexOf("COMMENT '") + 9) : StringUtil.lineToHump(key);
  63. comment = comment.trim();
  64. if (comment.contains(",")) {
  65. comment = comment.split(",")[0];
  66. }
  67. if (comment.contains(" ")) {
  68. comment = comment.split(" ")[0];
  69. }
  70. paramColumn.put("desc", comment);
  71. if (rowSql.toLowerCase().contains("not null")) {
  72. condition.put("name", comment);
  73. condition.put("inputType", "input");
  74. condition.put("code", StringUtil.lineToHump(key));
  75. condition.put("whereCondition", "equal");
  76. conditions.add(condition);
  77. paramColumn.put("desc", "必填," + comment);
  78. }
  79. String limit = rowSql.substring(rowSql.indexOf("(") + 1, rowSql.indexOf(")"));
  80. if (limit.contains(",")) {
  81. limit = limit.split(",")[0];
  82. }
  83. paramColumn.put("code", StringUtil.lineToHump(key));
  84. paramColumn.put("cnCode", comment);
  85. paramColumn.put("required", true);
  86. paramColumn.put("hasDefaultValue", false);
  87. paramColumn.put("inputType", "input");
  88. paramColumn.put("limit", "maxLength");
  89. paramColumn.put("limitParam", limit);
  90. paramColumn.put("limitErrInfo", comment + "不能超过" + limit);
  91. paramColumn.put("show", true);
  92. paramColumns.add(paramColumn);
  93. }
  94. param.put("columns", paramColumns);
  95. param.put("conditions", conditions);
  96. System.out.println(param.toJSONString());
  97. }
  98. }