Browse Source

从数据库获取表结构直接生成代码

main
QM303176530 4 years ago
parent
commit
b286ff6df6
  1. 1
      server/model/request/sys_autocode.go
  2. 3
      server/model/sys_auto_code.go
  3. 10
      server/resource/template/te/model.go.tpl
  4. 2
      server/service/sys_auto_code.go
  5. 9
      web/src/utils/stringFun.js
  6. 13
      web/src/view/systemTools/autoCode/component/fieldDialog.vue
  7. 49
      web/src/view/systemTools/autoCode/index.vue

1
server/model/request/sys_autocode.go

@ -11,5 +11,6 @@ type TableReq struct {
type ColumeReq struct {
ColumeName string `json:"columeName";gorm:"column:colume_name"`
DataType string `json:"dataType";gorm:"column:data_type"`
DataTypeLong string `json:"dataTypeLong";gorm:"column:data_type_long"`
ColumeComment string `json:"columeComment";gorm:"column:colume_comment"`
}

3
server/model/sys_auto_code.go

@ -3,6 +3,7 @@ package model
// 初始版本自动化代码工具
type AutoCodeStruct struct {
StructName string `json:"structName"`
TableName string `json:"tableName"`
PackageName string `json:"packageName"`
Abbreviation string `json:"abbreviation"`
Description string `json:"description"`
@ -15,6 +16,8 @@ type Field struct {
FieldDesc string `json:"fieldDesc"`
FieldType string `json:"fieldType"`
FieldJson string `json:"fieldJson"`
DataType string `json:"dataType"`
DataTypeLong string `json:"dataTypeLong"`
Comment string `json:"comment"`
ColumnName string `json:"columnName"`
FieldSearchType string `json:"fieldSearchType"`

10
server/resource/template/te/model.go.tpl

@ -9,8 +9,14 @@ import (
type {{.StructName}} struct {
gorm.Model {{- range .Fields}}
{{- if eq .FieldType "bool" }}
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:'{{.Comment}}'"`
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:'{{.Comment}}'{{- if .DataType -}};type:'{{.DataType}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}'{{- end -}}"`
{{- else }}
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:'{{.Comment}}'"`
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:'{{.Comment}}'{{- if .DataType -}};type:'{{.DataType}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}'{{- end -}}"`
{{- end }} {{- end }}
}
{{ if .TableName }}
func ({{.StructName}}) TableName() string {
return "{{.TableName}}"
}
{{ end }}

2
server/service/sys_auto_code.go

@ -129,6 +129,6 @@ func GetDB() (err error, DBNames []request.DBReq) {
}
func GetColume(tableName string, dbName string) (err error, Columes []request.ColumeReq) {
err = global.GVA_DB.Raw("select COLUMN_NAME as 'colume_name', DATA_TYPE as 'data_type', COLUMN_COMMENT as 'colume_comment' from information_schema.COLUMNS where table_name = ? and table_schema = ?", tableName, dbName).Scan(&Columes).Error
err = global.GVA_DB.Raw("SELECT COLUMN_NAME colume_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT colume_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columes).Error
return err, Columes
}

9
web/src/utils/stringFun.js

@ -10,4 +10,11 @@ export const toUpperCase = (str) => {
export const toSQLLine = (str) => {
if (str=="ID") return "ID"
return str.replace(/([A-Z])/g,"_$1").toLowerCase();
}
}
// 下划线转换驼峰
export const toHump = (name) => {
return name.replace(/\_(\w)/g, function(all, letter){
return letter.toUpperCase();
});
}

13
web/src/view/systemTools/autoCode/component/fieldDialog.vue

@ -29,7 +29,7 @@
</el-form-item>
<el-form-item label="Field数据类型" prop="fieldType">
<el-col :span="8">
<el-select v-model="dialogMiddle.fieldType" placeholder="请选择field数据类型" @change="getDbfdOptions">
<el-select v-model="dialogMiddle.fieldType" placeholder="请选择field数据类型" @change="getDbfdOptions" clearable >
<el-option
v-for="item in typeOptions"
:key="item.value"
@ -40,9 +40,9 @@
</el-col>
</el-form-item>
<el-form-item label="数据库字段类型" prop="dbFieldType">
<el-form-item label="数据库字段类型" prop="dataType">
<el-col :span="8">
<el-select :disabled="!dialogMiddle.fieldType" v-model="dialogMiddle.dbFieldType" placeholder="请选择数据库字段类型">
<el-select :disabled="!dialogMiddle.fieldType" v-model="dialogMiddle.dataType" placeholder="请选择数据库字段类型" clearable >
<el-option
v-for="item in dbfdOptions"
:key="item.label"
@ -52,9 +52,14 @@
</el-select>
</el-col>
</el-form-item>
<el-form-item label="数据库字段长度" prop="dataTypeLong">
<el-col :span="6">
<el-input :disabled="!dialogMiddle.dataType" v-model="dialogMiddle.dataTypeLong"></el-input>
</el-col>
</el-form-item>
<el-form-item label="Field查询条件" prop="fieldSearchType">
<el-col :span="8">
<el-select v-model="dialogMiddle.fieldSearchType" placeholder="请选择Field查询条件">
<el-select v-model="dialogMiddle.fieldSearchType" placeholder="请选择Field查询条件" clearable >
<el-option
v-for="item in typeSearchOptions"
:key="item.value"

49
web/src/view/systemTools/autoCode/index.vue

@ -1,12 +1,12 @@
<template>
<div>
<!-- 从数据库直接获取字段 -->
<el-collapse v-model="activeNames" @change="handleChange">
<el-collapse v-model="activeNames">
<el-collapse-item name="1">
<template slot="title">
<div :style="{fontSize:'16px',paddingLeft:'20px'}">
点这里从现有数据库创建代码
<i class="header-icon el-icon-info"></i>
<i class="header-icon el-icon-thumb"></i>
</div>
</template>
<el-form ref="getTableForm" :inline="true" :model="dbform" label-width="120px">
@ -48,6 +48,9 @@
<el-form-item label="Struct名称" prop="structName">
<el-input v-model="form.structName" placeholder="首字母自动转换大写"></el-input>
</el-form-item>
<el-form-item label="tableName" prop="tableName">
<el-input v-model="form.tableName" placeholder="指定表名(非必填)"></el-input>
</el-form-item>
<el-form-item label="Struct简称" prop="abbreviation">
<el-input v-model="form.abbreviation" placeholder="简称会作为入参对象名和路由group"></el-input>
</el-form-item>
@ -71,7 +74,8 @@
<el-table-column prop="fieldDesc" label="中文名"></el-table-column>
<el-table-column prop="fieldJson" label="FieldJson"></el-table-column>
<el-table-column prop="fieldType" label="Field数据类型" width="130"></el-table-column>
<el-table-column prop="dbFieldType" label="数据库字段类型" width="130"></el-table-column>
<el-table-column prop="dataType" label="数据库字段类型" width="130"></el-table-column>
<el-table-column prop="dataTypeLong" label="数据库字段长度" width="130"></el-table-column>
<el-table-column prop="columnName" label="数据库字段" width="130"></el-table-column>
<el-table-column prop="comment" label="数据库字段描述" width="130"></el-table-column>
<el-table-column prop="fieldSearchType" label="搜索条件" width="130"></el-table-column>
@ -106,13 +110,14 @@
</template>
</el-table-column>
</el-table>
<el-tag type="danger">id , created_at , updated_at , deleted_at 会自动生成请勿重复创建</el-tag>
<!-- 组件列表 -->
<div class="button-box clearflex">
<el-button @click="enterForm" type="primary">生成代码包</el-button>
</div>
<!-- 组件弹窗 -->
<el-dialog title="组件内容" :visible.sync="dialogFlag">
<FieldDialog :dialogMiddle="dialogMiddle" ref="fieldDialog" />
<FieldDialog v-if="dialogFlag" :dialogMiddle="dialogMiddle" ref="fieldDialog" />
<div slot="footer" class="dialog-footer">
<el-button @click="closeDialog"> </el-button>
<el-button type="primary" @click="enterDialog"> </el-button>
@ -125,15 +130,16 @@ const fieldTemplate = {
fieldName: "",
fieldDesc: "",
fieldType: "",
dbFieldType: "",
dataType: "",
fieldJson: "",
columnName: "",
dataTypeLong: "",
comment: "",
fieldSearchType: ""
};
import FieldDialog from "@/view/systemTools/autoCode/component/fieldDialog.vue";
import { toUpperCase } from "@/utils/stringFun.js";
import { toUpperCase, toHump } from "@/utils/stringFun.js";
import { createTemp, getDB, getTable, getColume } from "@/api/autoCode.js";
import { getDict } from "@/utils/dictionary";
@ -152,6 +158,7 @@ export default {
fdMap: {},
form: {
structName: "",
tableName: "",
packageName: "",
abbreviation: "",
description: "",
@ -301,7 +308,35 @@ export default {
this.dbform.tableName = "";
},
async getColume() {
await getColume(this.dbform);
const gormModelList = ["id", "created_at", "updated_at", "deleted_at"];
const res = await getColume(this.dbform);
if (res.code == 0) {
const tbHump = toHump(this.dbform.tableName);
this.form.structName = toUpperCase(tbHump);
this.form.tableName = this.dbform.tableName;
this.form.packageName = tbHump;
this.form.abbreviation = tbHump;
this.form.description = tbHump + "表";
this.form.autoCreateApiToSql = true;
this.form.fields = [];
res.data.columes &&
res.data.columes.map(item => {
if (!gormModelList.some(gormfd => gormfd == item.columeName)) {
const fbHump = toHump(item.columeName);
this.form.fields.push({
fieldName: toUpperCase(fbHump),
fieldDesc: item.columeComment || fbHump + "字段",
fieldType: this.fdMap[item.dataType],
dataType: item.dataType,
fieldJson: fbHump,
dataTypeLong: item.dataTypeLong,
columnName: item.columeName,
comment: item.columeComment,
fieldSearchType: ""
});
}
});
}
},
async setFdMap() {
const fdTpyes = ["string", "int", "bool", "float64", "time.Time"];

Loading…
Cancel
Save