123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <template>
- <div>
- <el-button style="margin-top:10px" @click="genEntrty">生成实体类</el-button>
- <el-button style="margin-top:10px" @click="genControler">生成接口类</el-button>
- <el-table
- :data="tableData.columnList"
- :header-cell-style="headerStyle"
- border
- stripe
- fit
- style="width: 100%;margin-top: 25px;"
- >
- <el-table-column fixed prop="columnName" label="字段名" width="150"></el-table-column>
- <el-table-column prop="description" min-width="200" label="说明"></el-table-column>
- <el-table-column prop="columnType" min-width="120" label="类型"></el-table-column>
- <el-table-column prop="charMaxLength" min-width="60" label="长度"></el-table-column>
- <el-table-column prop="nullable" align="center" label="不是 null"></el-table-column>
- <el-table-column prop="default" min-width="260" label="默认值"></el-table-column>
- </el-table>
- </div>
- </template>
- <script>
- import Vue from "vue";
- import ElementUI from "element-ui";
- import Axios from "axios";
- import "element-ui/lib/theme-chalk/index.css";
- import Opt from "../opt";
- import { SStringBuilder } from "@persagy-web/base";
- Vue.use(ElementUI);
- export default {
- data: function() {
- return {
- tableData: [],
- headerStyle: {
- backgroundColor: "#e1e4e5",
- color: "#2b2b2b",
- lineHeight: "30px"
- }, // 列表样式
- urlObj: {} // 路径参数
- };
- },
- components: {},
- created: function() {
- this.getData();
- },
- mounted: function() {
- },
- props: ["sys", "schema", "table","tableName"],
- computed: {},
- methods: {
- getData() {
- const origin = Location.origin
- Axios.get(
- `/${this.sys}/--database-doc--/table-info?schema=${
- this.schema
- }&table=${this.table}`,
- {}
- ).then(res => {
- this.tableData = res.data;
- });
- },
- //生成实体类
- genEntrty() {
- this.getUrlData();
- let strBuild = new SStringBuilder();
- const table = this.toHump(this.table);
- strBuild.append(
- `package com.persagy.server.models
- import com.persagy.service.models.SBaseEntity
- import io.swagger.annotations.ApiModel
- import io.swagger.annotations.ApiModelProperty
- import java.io.Serializable
- import java.util.*
- import javax.persistence.Column
- import javax.persistence.Table
- /**
- * ${this.tableName}信息实体类
- *
- * @author 自动生成
- */
- @ApiModel(description = "${this.tableName}信息实体类")
- @Table(name = "${this.schema}.${this.table}")
- open class ${table} : Serializable,SBaseEntity() {
- `
- );
- this.tableData.columnList.forEach(element => {
- console.log(element)
- if(element.columnName != "create_time" && element.columnName != "last_update") {
- strBuild.append(` /** ${element.description} */`);
- if (element.nullable == "YES") {
- strBuild.append(
- ` @ApiModelProperty(value = "${element.description}")`
- );
- } else {
- strBuild.append(
- ` @ApiModelProperty(value = "${element.description}", required = true)`
- );
- }
- strBuild.append(` @Column(name = "${element.columnName}")`);
- strBuild.append(
- ` var ${this.toSmallHump(element.columnName)}: ${this.typeToName(
- element.columnType
- )}? = null\n`
- );
- }
- });
- strBuild.append(`} // Class ${table}`);
- this.download(`${table}.kt`, strBuild.toString());
- },
- //生成接口类
- genControler() {
- this.getUrlData();
- let strBuild = new SStringBuilder();
- const className = `${this.toHump(this.table)}Controller`;
- strBuild.append(`package com.persagy.server.controller
- import com.persagy.mybatis.SMybatisDao
- import com.persagy.server.models.Test01
- import com.persagy.service.SObjectService
- import com.persagy.service.models.requests.SCountRequest
- import com.persagy.service.models.requests.SCreateRequest
- import com.persagy.service.models.requests.SQueryRequest
- import com.persagy.service.models.requests.SUpdateRequest
- import com.persagy.service.models.responses.SBaseResponse
- import com.persagy.service.models.responses.SCountResponse
- import com.persagy.service.models.responses.SCreateResponse
- import com.persagy.service.models.responses.SQueryResponse
- import io.swagger.annotations.Api
- import io.swagger.annotations.ApiOperation
- import org.slf4j.LoggerFactory
- import org.springframework.web.bind.annotation.PostMapping
- import org.springframework.web.bind.annotation.RequestBody
- import org.springframework.web.bind.annotation.RequestMapping
- import org.springframework.web.bind.annotation.RestController
- /**
- * ${this.tableName}接口
- *
- * @author 自动生成
- */
- @Api(tags = ["${this.tableName}"])
- @RestController
- @RequestMapping("/${this.table}")
- open class ${className} {
- companion object {
- /** 日志 */
- private val logger = LoggerFactory.getLogger(${className}::class.java)
- } // Companion object
- /** 服务 */
- val service = SObjectService(SMybatisDao(${this.toHump(this.table)}::class.java))
- /**
- * 创建${this.tableName}
- *
- * @param request ${this.tableName}对象列表
- * @return 创建结果信息
- */
- @ApiOperation(value = "创建${this.tableName}信息", notes = "")
- @PostMapping(value = ["/create"])
- fun create(@RequestBody request: SCreateRequest<${this.toHump(
- this.table
- )}>): SCreateResponse<${this.toHump(this.table)}> {
- return service.createList( request)
- } // Function create()
- /**
- * 根据id删除${this.tableName}
- *
- * @param idList id数组
- * @return 删除的结果信息
- */
- @ApiOperation(value = "根据id删除${this.tableName}信息", notes = "")
- @PostMapping(value = ["/delete"])
- fun delete(@RequestBody idList: ArrayList<${this.toHump(
- this.table
- )}>): SBaseResponse {
- return service.deleteByKeysList(idList)
- } // Function delete()
- /**
- * 更新${this.tableName}信息
- *
- * @param request 更新的内容对象
- * @return 更新的结果
- */
- @ApiOperation(value = "更新${this.tableName}信息", notes = "")
- @PostMapping(value = ["/update"])
- fun update(@RequestBody request: SUpdateRequest<${this.toHump(
- this.table
- )}>): SBaseResponse {
- return service.updateList(request)
- } // Function update()
- /**
- * 查询${this.tableName}信息
- *
- * @param request 查询信息条件
- * @return 查询结果
- */
- @ApiOperation(value = "查询${this.tableName}信息", notes="")
- @PostMapping(value = ["/query"])
- fun query(@RequestBody request: SQueryRequest): SQueryResponse<${this.toHump(
- this.table
- )}> {
- return service.pageQuery(request)
- } // Function query()
- /**
- * 根据条件查询统计数量
- */
- @ApiOperation(value = "根据条件查询统计数量", notes = "")
- @PostMapping(value = ["/count"])
- fun count(@RequestBody request: SCountRequest): SCountResponse {
- return service.count(request)
- } // Function count()
- `);
- strBuild.append(`} // Class ${className}`);
- this.download(`${className}.kt`, strBuild.toString());
- },
- // 数据包类型到实体类的转换
- typeToName(name) {
- const obj = {
- varchar: "String",
- char: "String",
- timestamptz: "Date",
- timestamp: "Date",
- text: "String",
- bool: "Boolean",
- int4: "Int",
- date: "Date",
- int2: "Int",
- json: "HashMap<String, Any?>",
- jsonb: "HashMap<String, Any?>"
- };
- if (name) {
- return obj[name] ? obj[name] : "String";
- } else {
- return "String";
- }
- },
- //转大驼峰
- toHump(str) {
- var arr = str.split("_");
- for (var i = 0; i < arr.length; i++) {
- arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
- }
- return arr.join("");
- },
- //转小驼峰
- toSmallHump(str) {
- var arr = str.split("_");
- for (var i = 1; i < arr.length; i++) {
- arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
- }
- return arr.join("");
- },
- // 获取路径名称以及路径type
- getUrlData() {
- const dataArr = Location.href.split("#")[1].split("-");
- let obj = {};
- if (dataArr.length == 2) {
- obj = {
- urlType: dataArr[1],
- urlName: decodeURIComponent(dataArr[0])
- };
- } else {
- let urlName = "";
- let lengthNum = dataArr.length;
- dataArr.forEach((item, i) => {
- if (i + 2 <= lengthNum) {
- urlName = urlName + "-" + item;
- }
- });
- obj = {
- urlType: dataArr[lengthNum-1],
- urlName: decodeURIComponent(urlName)
- };
- }
- this.urlObj = obj;
- },
- // 下载文件
- download(filename, text) {
- var element = document.createElement("a");
- element.setAttribute(
- "href",
- "data:text/plain;charset=utf-8," + encodeURIComponent(text)
- );
- element.setAttribute("download", filename);
- element.style.display = "none";
- document.body.appendChild(element);
- element.click();
- document.body.removeChild(element);
- }
- }
- };
- </script>
- <style scoped>
- /deep/ table {
- margin: 0;
- }
- </style>
|