TableToJsonWeb.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 `s_store` (\n" +
  8. " `store_id` varchar(30) NOT NULL COMMENT '商店ID',\n" +
  9. " `name` varchar(100) NOT NULL COMMENT '名称',\n" +
  10. " `address` varchar(200) NOT NULL COMMENT '地址',\n" +
  11. " `tel` varchar(11) NOT NULL COMMENT '电话',\n" +
  12. " `nearby_landmarks` varchar(200) DEFAULT NULL COMMENT '地标',\n" +
  13. ")";
  14. public static void main(String[] args) {
  15. String templateName = "物业公司"; //业务名称
  16. String templateCode = "propertyCompany"; //表名大写
  17. String templateKey = "storeId"; //表主键
  18. String templateKeyName = "编号";//主键说明
  19. String searchCode = "storeId"; //分片字段
  20. String searchName = "编号"; //分片字段说明
  21. String directories = "admin"; //前端生成到那个目录下
  22. // templateName 业务名称 业务编码名称生成后文件名 templateCode 主键 templateKey
  23. // 业务主键名称 templateKeyName=templateName+ID 主机驼峰 searchCode 主键名称 searchName
  24. // directories 放在前端那个目录下
  25. String newSql = createTableSql.substring(createTableSql.indexOf("(") + 1, createTableSql.lastIndexOf(")"));
  26. String tableName = createTableSql.substring(createTableSql.indexOf("TABLE") + 5, createTableSql.indexOf("("));
  27. tableName = tableName.replaceAll("`", "").trim();
  28. newSql = newSql.replaceAll("\n", "");
  29. String[] rowSqls = newSql.split("',");
  30. JSONObject param = new JSONObject();
  31. param.put("templateName", templateName);
  32. param.put("templateCode", templateCode);
  33. param.put("templateKey", templateKey);
  34. param.put("templateKeyName", templateKeyName);
  35. param.put("searchCode", searchCode);
  36. param.put("searchName", searchName);
  37. param.put("directories", directories);
  38. JSONObject paramColumn = null;
  39. JSONArray conditions = new JSONArray();
  40. JSONArray paramColumns = new JSONArray();
  41. JSONObject condition = null;
  42. String key = "";
  43. for (String rowSql : rowSqls) {
  44. condition = new JSONObject();
  45. paramColumn = new JSONObject();
  46. key = rowSql.trim();
  47. key = key.substring(0, key.indexOf(" "));
  48. key = key.replaceAll("`", "");
  49. if ("UNIQUE".equals(key)) {
  50. continue;
  51. }
  52. if ("KEY".equals(key)) {
  53. continue;
  54. }
  55. if ("b_id".equals(key)) {
  56. continue;
  57. }
  58. if ("create_time".equals(key)) {
  59. continue;
  60. }
  61. if ("status_cd".equals(key)) {
  62. continue;
  63. }
  64. String comment = rowSql.contains("COMMENT") ? rowSql.substring(rowSql.indexOf("COMMENT '") + 9) : StringUtil.lineToHump(key);
  65. comment = comment.trim();
  66. if (comment.contains(",")) {
  67. comment = comment.split(",")[0];
  68. }
  69. if (comment.contains(" ")) {
  70. comment = comment.split(" ")[0];
  71. }
  72. paramColumn.put("desc", comment);
  73. if (rowSql.toLowerCase().contains("not null")) {
  74. condition.put("name", comment);
  75. condition.put("inputType", "input");
  76. condition.put("code", StringUtil.lineToHump(key));
  77. condition.put("whereCondition", "equal");
  78. conditions.add(condition);
  79. paramColumn.put("desc", "必填," + comment);
  80. }
  81. String limit = rowSql.substring(rowSql.indexOf("(") + 1, rowSql.indexOf(")"));
  82. if (limit.contains(",")) {
  83. limit = limit.split(",")[0];
  84. }
  85. paramColumn.put("code", StringUtil.lineToHump(key));
  86. paramColumn.put("cnCode", comment);
  87. paramColumn.put("required", true);
  88. paramColumn.put("hasDefaultValue", false);
  89. paramColumn.put("inputType", "input");
  90. paramColumn.put("limit", "maxLength");
  91. paramColumn.put("limitParam", limit);
  92. paramColumn.put("limitErrInfo", comment + "不能超过" + limit);
  93. paramColumn.put("show", true);
  94. paramColumns.add(paramColumn);
  95. }
  96. param.put("columns", paramColumns);
  97. param.put("conditions", conditions);
  98. System.out.println(param.toJSONString());
  99. }
  100. }