代码生成 支持数据源选择

This commit is contained in:
吕金泽 2022-05-31 19:12:12 +08:00
parent f58c3c0787
commit 2d9f157032
5 changed files with 159 additions and 20 deletions

View File

@ -0,0 +1,103 @@
{
"properties" : { },
"id" : "600d13f5cf6d4c3496da2764045c2ac6",
"script" : null,
"groupId" : "f2538e0f370a4cabab0ae920f5e77ae7",
"name" : "所有数据源",
"createTime" : null,
"updateTime" : 1653990520715,
"lock" : null,
"createBy" : null,
"updateBy" : null,
"path" : "/database",
"method" : "GET",
"parameters" : [ ],
"options" : [ ],
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : "{\n \"code\": 200,\n \"message\": \"success\",\n \"data\": null,\n \"timestamp\": 1653990517813,\n \"executeTime\": 8\n}",
"description" : null,
"requestBodyDefinition" : null,
"responseBodyDefinition" : {
"name" : "",
"value" : "",
"description" : "",
"required" : false,
"dataType" : "Object",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ {
"name" : "code",
"value" : "200",
"description" : "",
"required" : false,
"dataType" : "Integer",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ ]
}, {
"name" : "message",
"value" : "success",
"description" : "",
"required" : false,
"dataType" : "String",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ ]
}, {
"name" : "data",
"value" : "null",
"description" : "",
"required" : false,
"dataType" : "Array",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ ]
}, {
"name" : "timestamp",
"value" : "1653990517813",
"description" : "",
"required" : false,
"dataType" : "Long",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ ]
}, {
"name" : "executeTime",
"value" : "8",
"description" : "",
"required" : false,
"dataType" : "Integer",
"type" : null,
"defaultValue" : null,
"validateType" : "",
"error" : "",
"expression" : "",
"children" : [ ]
} ]
}
}
================================
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource
return MagicDynamicDataSource.datasources().map(it => {
return {
label: it == '' ? '默认数据源' : it,
value: it
}
})

View File

@ -5,7 +5,7 @@
"groupId" : "f2538e0f370a4cabab0ae920f5e77ae7",
"name" : "执行生成",
"createTime" : null,
"updateTime" : 1652170746742,
"updateTime" : 1653992145291,
"lock" : null,
"createBy" : null,
"updateBy" : null,
@ -325,7 +325,7 @@ columns.forEach(it => {
}
})
var commonField = db.select("""
var commonField = db[datasource].select("""
SELECT
column_name
FROM
@ -334,7 +334,7 @@ var commonField = db.select("""
table_name = #{tableName} AND column_name IN ( 'is_del', 'create_by', 'create_date', 'update_by', 'update_date' ) AND table_schema = DATABASE()
""")
commonField = commonField.map(it => it.columnName)
var primary = db.selectValue("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = #{tableName} and column_key = 'PRI' and table_schema = database() limit 1")
var primary = db[datasource].selectValue("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = #{tableName} and column_key = 'PRI' and table_schema = database() limit 1")
var primaryLowerCamelCase = primary.replace(/([A-Z])/g,"_$1").toLowerCase()
var logic = ''
@ -344,12 +344,14 @@ if(commonField.contains('is_del')){
}
wheres = wheres.substring(0,wheres.length() - 1)
var datasourceScript = datasource ? `.${datasource}` : '';
ApiInfo listApi = new ApiInfo()
listApi.setName("列表")
listApi.setPath("/list")
listApi.setMethod("POST")
listApi.setGroupId(businessGroup.getId())
listApi.setScript(`return db.page("""
listApi.setScript(`return db${datasourceScript}.page("""
select ${primary},${listFields} from ${tableName} where 1=1
${wheres}
""")`)
@ -366,7 +368,7 @@ saveApi.setName("保存")
saveApi.setPath("/save")
saveApi.setMethod("POST")
saveApi.setGroupId(businessGroup.getId())
saveApi.setScript(`return db.table('${tableName}').primary('${primary}').${saveMethod}`)
saveApi.setScript(`return db${datasourceScript}.table('${tableName}').primary('${primary}').${saveMethod}`)
saveApi.setOption([new Option('wrap_request_parameter', 'data'), new Option('permission', `${permissionPrefix}:save`, '允许拥有该权限的访问')])
MagicResourceService.saveFile(saveApi)
@ -375,7 +377,7 @@ deleteApi.setName("删除")
deleteApi.setPath("/delete")
deleteApi.setMethod("DELETE")
deleteApi.setGroupId(businessGroup.getId())
deleteApi.setScript(`return db.table('${tableName}')${logic}.where().eq('${primary}', ${primaryLowerCamelCase}).delete()`)
deleteApi.setScript(`return db${datasourceScript}.table('${tableName}')${logic}.where().eq('${primary}', ${primaryLowerCamelCase}).delete()`)
deleteApi.setOption([new Option('permission', `${permissionPrefix}:delete`, '允许拥有该权限的访问')])
MagicResourceService.saveFile(deleteApi)
@ -386,7 +388,7 @@ getApi.setName("详情")
getApi.setPath("/get")
getApi.setMethod("GET")
getApi.setGroupId(businessGroup.getId())
getApi.setScript(`return db.selectOne("""
getApi.setScript(`return db${datasourceScript}.selectOne("""
select ${primary},${saveFields} from ${tableName} where ${primary} = #{${primaryLowerCamelCase}}
""")`)
getApi.setOption([new Option('permission', `${permissionPrefix}:view`, '允许拥有该权限的访问')])

View File

@ -5,7 +5,7 @@
"groupId" : "f2538e0f370a4cabab0ae920f5e77ae7",
"name" : "查出所有表",
"createTime" : null,
"updateTime" : 1648389273779,
"updateTime" : 1653990626738,
"lock" : null,
"createBy" : null,
"updateBy" : null,
@ -16,7 +16,7 @@
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : "{\n \"code\": 200,\n \"message\": \"success\",\n \"data\": [{\n \"label\": \"magic_backup_record_v2()\",\n \"value\": \"magic_backup_record_v2\"\n }, {\n \"label\": \"sys_dict(字典表)\",\n \"value\": \"sys_dict\"\n }, {\n \"label\": \"sys_dict_items(字典项表)\",\n \"value\": \"sys_dict_items\"\n }, {\n \"label\": \"sys_file(文件表)\",\n \"value\": \"sys_file\"\n }, {\n \"label\": \"sys_gen_info(生成表信息)\",\n \"value\": \"sys_gen_info\"\n }, {\n \"label\": \"sys_gen_test()\",\n \"value\": \"sys_gen_test\"\n }, {\n \"label\": \"sys_login_log()\",\n \"value\": \"sys_login_log\"\n }, {\n \"label\": \"sys_menu(菜单表)\",\n \"value\": \"sys_menu\"\n }, {\n \"label\": \"sys_office(组织机构表)\",\n \"value\": \"sys_office\"\n }, {\n \"label\": \"sys_oper_log(操作日志)\",\n \"value\": \"sys_oper_log\"\n }, {\n \"label\": \"sys_permission_code(权限code表)\",\n \"value\": \"sys_permission_code\"\n }, {\n \"label\": \"sys_role(角色表)\",\n \"value\": \"sys_role\"\n }, {\n \"label\": \"sys_role_menu(角色和菜单关联表)\",\n \"value\": \"sys_role_menu\"\n }, {\n \"label\": \"sys_role_office(用户组织机构关联表)\",\n \"value\": \"sys_role_office\"\n }, {\n \"label\": \"sys_test_data()\",\n \"value\": \"sys_test_data\"\n }, {\n \"label\": \"sys_user(用户表)\",\n \"value\": \"sys_user\"\n }, {\n \"label\": \"sys_user_code(用户权限code关联表)\",\n \"value\": \"sys_user_code\"\n }, {\n \"label\": \"sys_user_role(用户与角色关联表)\",\n \"value\": \"sys_user_role\"\n }],\n \"timestamp\": 1648372075872,\n \"executeTime\": 24\n}",
"responseBody" : "{\n \"code\": 200,\n \"message\": \"success\",\n \"data\": [{\n \"label\": \"magic_backup_record_v2()\",\n \"value\": \"magic_backup_record_v2\"\n }, {\n \"label\": \"sys_configure(配置中心)\",\n \"value\": \"sys_configure\"\n }, {\n \"label\": \"sys_dict(字典表)\",\n \"value\": \"sys_dict\"\n }, {\n \"label\": \"sys_dict_items(字典项表)\",\n \"value\": \"sys_dict_items\"\n }, {\n \"label\": \"sys_file(文件表)\",\n \"value\": \"sys_file\"\n }, {\n \"label\": \"sys_gen_info(生成表信息)\",\n \"value\": \"sys_gen_info\"\n }, {\n \"label\": \"sys_login_log()\",\n \"value\": \"sys_login_log\"\n }, {\n \"label\": \"sys_menu(菜单表)\",\n \"value\": \"sys_menu\"\n }, {\n \"label\": \"sys_office(组织机构表)\",\n \"value\": \"sys_office\"\n }, {\n \"label\": \"sys_oper_log(操作日志)\",\n \"value\": \"sys_oper_log\"\n }, {\n \"label\": \"sys_permission_code(权限code表)\",\n \"value\": \"sys_permission_code\"\n }, {\n \"label\": \"sys_role(角色表)\",\n \"value\": \"sys_role\"\n }, {\n \"label\": \"sys_role_menu(角色和菜单关联表)\",\n \"value\": \"sys_role_menu\"\n }, {\n \"label\": \"sys_role_office(用户组织机构关联表)\",\n \"value\": \"sys_role_office\"\n }, {\n \"label\": \"sys_test_data()\",\n \"value\": \"sys_test_data\"\n }, {\n \"label\": \"sys_user(用户表)\",\n \"value\": \"sys_user\"\n }, {\n \"label\": \"sys_user_code(用户权限code关联表)\",\n \"value\": \"sys_user_code\"\n }, {\n \"label\": \"sys_user_role(用户与角色关联表)\",\n \"value\": \"sys_user_role\"\n }, {\n \"label\": \"t_data_test(测试生成)\",\n \"value\": \"t_data_test\"\n }],\n \"timestamp\": 1653990585569,\n \"executeTime\": 11\n}",
"description" : null,
"requestBodyDefinition" : null,
"responseBodyDefinition" : {
@ -104,7 +104,7 @@
} ]
}, {
"name" : "timestamp",
"value" : "1648372075872",
"value" : "1653990585569",
"description" : "",
"required" : false,
"dataType" : "Long",
@ -116,7 +116,7 @@
"children" : [ ]
}, {
"name" : "executeTime",
"value" : "24",
"value" : "11",
"description" : "",
"required" : false,
"dataType" : "Object",
@ -130,6 +130,6 @@
}
}
================================
return db.select("""
return db[datasource].select("""
select table_name, table_comment from information_schema.tables where table_schema = database()
""").map(it => { label: it.tableName + `(${it.tableComment})`, value: it.tableName })

View File

@ -5,7 +5,7 @@
"groupId" : "f2538e0f370a4cabab0ae920f5e77ae7",
"name" : "根据表名查出所有列",
"createTime" : null,
"updateTime" : 1648484373195,
"updateTime" : 1653991292310,
"lock" : null,
"createBy" : null,
"updateBy" : null,
@ -191,7 +191,7 @@
}
================================
return {
columns: db.select("""
columns: db[datasource].select("""
SELECT
column_name,
column_comment,

View File

@ -3,12 +3,17 @@
<el-tabs v-model="activeName" @tab-click="handleClick">
<el-tab-pane label="表信息" name="basic">
<el-row :gutter="24">
<el-col :span="12">
<el-col :span="8">
<el-form-item label="数据源" prop="datasource">
<mb-select v-model="genInfo.datasource" url="/system/code/gen/database" />
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="表名" prop="tableName">
<mb-select v-model="genInfo.tableName" :options="tables" />
</el-form-item>
</el-col>
<el-col :span="12">
<el-col :span="8">
<el-form-item label="描述" prop="tableComment">
<el-input v-model="genInfo.tableComment" />
</el-form-item>
@ -92,7 +97,7 @@
</template>
<script setup>
import {reactive, ref, watch, getCurrentInstance, onMounted} from 'vue'
import {reactive, ref, watch, getCurrentInstance, onMounted, nextTick} from 'vue'
import genCode from '@/scripts/gen/gen-mb-list.js'
import { ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance()
@ -126,6 +131,7 @@
'info.businessPath': [{ required: true, message: '请输入功能路径', trigger: 'change' }, { validator: validatePath }]
})
const genInfo = ref({
datasource: '',
tableName: '',
tableComment: '',
info: {
@ -139,14 +145,41 @@
columns: []
})
async function watchTableName(){
await proxy.$get('/system/code/gen/tables').then(res => {
function datasourceChange(){
proxy.$get('/system/code/gen/tables?datasource='+genInfo.value.datasource).then(res => {
tables.value = res.data
})
}
watch(() => genInfo.value.datasource, () => {
datasourceChange()
genInfo.value.tableName = ''
})
function formInitClearValidate(){
genInfo.value.tableComment = ''
genInfo.value.info.moduleName = '';
genInfo.value.info.modulePath = '';
genInfo.value.info.businessName = '';
genInfo.value.info.businessPath = '';
genInfo.value.info.template = 'singleTable';
genInfo.value.info.pid = '';
genInfo.value.columns = []
setTimeout(() => {
dataForm.value.clearValidate()
},1)
}
function watchTableName(){
datasourceChange()
watch(() => genInfo.value.tableName, (value) => {
if(!value){
formInitClearValidate()
return
}
genInfo.value.tableComment = tables.value.filter(it => it.value == value)[0].label.replace(value, '').replace('(','').replace(')','')
genInfo.value.columns = []
proxy.$get('/system/code/gen/columns', { tableName: value }).then(res => {
proxy.$get('/system/code/gen/columns?datasource='+genInfo.value.datasource, { tableName: value }).then(res => {
var columns = res.data.columns
var primary = res.data.primary
columns.forEach(it => {
@ -318,6 +351,7 @@
formData.info = JSON.stringify(genInfo.value.info)
formData.columns = JSON.stringify(genInfo.value.columns)
formData.componentScript = genCode(genInfo.value.info.modulePath+genInfo.value.info.businessPath, genInfo.value.columns)
formData.datasource = genInfo.value.datasource
proxy.$post('/system/code/gen/execute', formData).then((res) => {
if(res.data == 1){
proxy.$notify({