TableDesig.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <template>
  2. <div>
  3. <el-button style="margin-top:10px" @click="genEntrty">生成实体类</el-button>
  4. <el-button style="margin-top:10px" @click="genControler">生成接口类</el-button>
  5. <el-table
  6. :data="tableData.columnList"
  7. :header-cell-style="headerStyle"
  8. border
  9. stripe
  10. fit
  11. style="width: 100%;margin-top: 25px;"
  12. >
  13. <el-table-column fixed prop="columnName" label="字段名" width="150"></el-table-column>
  14. <el-table-column prop="description" min-width="200" label="说明"></el-table-column>
  15. <el-table-column prop="columnType" min-width="120" label="类型"></el-table-column>
  16. <el-table-column prop="charMaxLength" min-width="60" label="长度"></el-table-column>
  17. <el-table-column prop="nullable" align="center" label="不是 null"></el-table-column>
  18. <el-table-column prop="default" min-width="260" label="默认值"></el-table-column>
  19. </el-table>
  20. </div>
  21. </template>
  22. <script>
  23. import Vue from "vue";
  24. import ElementUI from "element-ui";
  25. import Axios from "axios";
  26. import "element-ui/lib/theme-chalk/index.css";
  27. import Opt from "../opt";
  28. import { SStringBuilder } from "@persagy-web/base";
  29. Vue.use(ElementUI);
  30. export default {
  31. data: function() {
  32. return {
  33. tableData: [],
  34. headerStyle: {
  35. backgroundColor: "#e1e4e5",
  36. color: "#2b2b2b",
  37. lineHeight: "30px"
  38. }, // 列表样式
  39. urlObj: {} // 路径参数
  40. };
  41. },
  42. components: {},
  43. created: function() {
  44. this.getData();
  45. },
  46. mounted: function() {
  47. },
  48. props: ["sys", "schema", "table","tableName"],
  49. computed: {},
  50. methods: {
  51. getData() {
  52. const origin = Location.origin
  53. Axios.get(
  54. `/${this.sys}/--database-doc--/table-info?schema=${
  55. this.schema
  56. }&table=${this.table}`,
  57. {}
  58. ).then(res => {
  59. this.tableData = res.data;
  60. });
  61. },
  62. //生成实体类
  63. genEntrty() {
  64. this.getUrlData();
  65. let strBuild = new SStringBuilder();
  66. const table = this.toHump(this.table);
  67. strBuild.append(
  68. `package com.persagy.server.models
  69. import com.persagy.service.models.SBaseEntity
  70. import io.swagger.annotations.ApiModel
  71. import io.swagger.annotations.ApiModelProperty
  72. import java.io.Serializable
  73. import java.util.*
  74. import javax.persistence.Column
  75. import javax.persistence.Table
  76. /**
  77. * ${this.tableName}信息实体类
  78. *
  79. * @author 自动生成
  80. */
  81. @ApiModel(description = "${this.tableName}信息实体类")
  82. @Table(name = "${this.schema}.${this.table}")
  83. open class ${table} : Serializable,SBaseEntity() {
  84. `
  85. );
  86. this.tableData.columnList.forEach(element => {
  87. console.log(element)
  88. if(element.columnName != "create_time" && element.columnName != "last_update") {
  89. strBuild.append(` /** ${element.description} */`);
  90. if (element.nullable == "YES") {
  91. strBuild.append(
  92. ` @ApiModelProperty(value = "${element.description}")`
  93. );
  94. } else {
  95. strBuild.append(
  96. ` @ApiModelProperty(value = "${element.description}", required = true)`
  97. );
  98. }
  99. strBuild.append(` @Column(name = "${element.columnName}")`);
  100. strBuild.append(
  101. ` var ${this.toSmallHump(element.columnName)}: ${this.typeToName(
  102. element.columnType
  103. )}? = null\n`
  104. );
  105. }
  106. });
  107. strBuild.append(`} // Class ${table}`);
  108. this.download(`${table}.kt`, strBuild.toString());
  109. },
  110. //生成接口类
  111. genControler() {
  112. this.getUrlData();
  113. let strBuild = new SStringBuilder();
  114. const className = `${this.toHump(this.table)}Controller`;
  115. strBuild.append(`package com.persagy.server.controller
  116. import com.persagy.mybatis.SMybatisDao
  117. import com.persagy.server.models.Test01
  118. import com.persagy.service.SObjectService
  119. import com.persagy.service.models.requests.SCountRequest
  120. import com.persagy.service.models.requests.SCreateRequest
  121. import com.persagy.service.models.requests.SQueryRequest
  122. import com.persagy.service.models.requests.SUpdateRequest
  123. import com.persagy.service.models.responses.SBaseResponse
  124. import com.persagy.service.models.responses.SCountResponse
  125. import com.persagy.service.models.responses.SCreateResponse
  126. import com.persagy.service.models.responses.SQueryResponse
  127. import io.swagger.annotations.Api
  128. import io.swagger.annotations.ApiOperation
  129. import org.slf4j.LoggerFactory
  130. import org.springframework.web.bind.annotation.PostMapping
  131. import org.springframework.web.bind.annotation.RequestBody
  132. import org.springframework.web.bind.annotation.RequestMapping
  133. import org.springframework.web.bind.annotation.RestController
  134. /**
  135. * ${this.tableName}接口
  136. *
  137. * @author 自动生成
  138. */
  139. @Api(tags = ["${this.tableName}"])
  140. @RestController
  141. @RequestMapping("/${this.table}")
  142. open class ${className} {
  143. companion object {
  144. /** 日志 */
  145. private val logger = LoggerFactory.getLogger(${className}::class.java)
  146. } // Companion object
  147. /** 服务 */
  148. val service = SObjectService(SMybatisDao(${this.toHump(this.table)}::class.java))
  149. /**
  150. * 创建${this.tableName}
  151. *
  152. * @param request ${this.tableName}对象列表
  153. * @return 创建结果信息
  154. */
  155. @ApiOperation(value = "创建${this.tableName}信息", notes = "")
  156. @PostMapping(value = ["/create"])
  157. fun create(@RequestBody request: SCreateRequest<${this.toHump(
  158. this.table
  159. )}>): SCreateResponse<${this.toHump(this.table)}> {
  160. return service.createList( request)
  161. } // Function create()
  162. /**
  163. * 根据id删除${this.tableName}
  164. *
  165. * @param idList id数组
  166. * @return 删除的结果信息
  167. */
  168. @ApiOperation(value = "根据id删除${this.tableName}信息", notes = "")
  169. @PostMapping(value = ["/delete"])
  170. fun delete(@RequestBody idList: ArrayList<${this.toHump(
  171. this.table
  172. )}>): SBaseResponse {
  173. return service.deleteByKeysList(idList)
  174. } // Function delete()
  175. /**
  176. * 更新${this.tableName}信息
  177. *
  178. * @param request 更新的内容对象
  179. * @return 更新的结果
  180. */
  181. @ApiOperation(value = "更新${this.tableName}信息", notes = "")
  182. @PostMapping(value = ["/update"])
  183. fun update(@RequestBody request: SUpdateRequest<${this.toHump(
  184. this.table
  185. )}>): SBaseResponse {
  186. return service.updateList(request)
  187. } // Function update()
  188. /**
  189. * 查询${this.tableName}信息
  190. *
  191. * @param request 查询信息条件
  192. * @return 查询结果
  193. */
  194. @ApiOperation(value = "查询${this.tableName}信息", notes="")
  195. @PostMapping(value = ["/query"])
  196. fun query(@RequestBody request: SQueryRequest): SQueryResponse<${this.toHump(
  197. this.table
  198. )}> {
  199. return service.pageQuery(request)
  200. } // Function query()
  201. /**
  202. * 根据条件查询统计数量
  203. */
  204. @ApiOperation(value = "根据条件查询统计数量", notes = "")
  205. @PostMapping(value = ["/count"])
  206. fun count(@RequestBody request: SCountRequest): SCountResponse {
  207. return service.count(request)
  208. } // Function count()
  209. `);
  210. strBuild.append(`} // Class ${className}`);
  211. this.download(`${className}.kt`, strBuild.toString());
  212. },
  213. // 数据包类型到实体类的转换
  214. typeToName(name) {
  215. const obj = {
  216. varchar: "String",
  217. char: "String",
  218. timestamptz: "Date",
  219. timestamp: "Date",
  220. text: "String",
  221. bool: "Boolean",
  222. int4: "Int",
  223. date: "Date",
  224. int2: "Int",
  225. json: "HashMap<String, Any?>",
  226. jsonb: "HashMap<String, Any?>"
  227. };
  228. if (name) {
  229. return obj[name] ? obj[name] : "String";
  230. } else {
  231. return "String";
  232. }
  233. },
  234. //转大驼峰
  235. toHump(str) {
  236. var arr = str.split("_");
  237. for (var i = 0; i < arr.length; i++) {
  238. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  239. }
  240. return arr.join("");
  241. },
  242. //转小驼峰
  243. toSmallHump(str) {
  244. var arr = str.split("_");
  245. for (var i = 1; i < arr.length; i++) {
  246. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  247. }
  248. return arr.join("");
  249. },
  250. // 获取路径名称以及路径type
  251. getUrlData() {
  252. const dataArr = Location.href.split("#")[1].split("-");
  253. let obj = {};
  254. if (dataArr.length == 2) {
  255. obj = {
  256. urlType: dataArr[1],
  257. urlName: decodeURIComponent(dataArr[0])
  258. };
  259. } else {
  260. let urlName = "";
  261. let lengthNum = dataArr.length;
  262. dataArr.forEach((item, i) => {
  263. if (i + 2 <= lengthNum) {
  264. urlName = urlName + "-" + item;
  265. }
  266. });
  267. obj = {
  268. urlType: dataArr[lengthNum-1],
  269. urlName: decodeURIComponent(urlName)
  270. };
  271. }
  272. this.urlObj = obj;
  273. },
  274. // 下载文件
  275. download(filename, text) {
  276. var element = document.createElement("a");
  277. element.setAttribute(
  278. "href",
  279. "data:text/plain;charset=utf-8," + encodeURIComponent(text)
  280. );
  281. element.setAttribute("download", filename);
  282. element.style.display = "none";
  283. document.body.appendChild(element);
  284. element.click();
  285. document.body.removeChild(element);
  286. }
  287. }
  288. };
  289. </script>
  290. <style scoped>
  291. /deep/ table {
  292. margin: 0;
  293. }
  294. </style>