Browse Source

代码生成器模板 model curd初步完工

main
pixel 5 years ago
parent
commit
43fa2d76ab
  1. 18
      QMPlusServer/model/autoCodeModel/autoCode.go
  2. 27
      QMPlusServer/tpl/te/struct.go.tpl
  3. 3
      QMPlusVuePage/src/view/superAdmin/autoCode/index.vue

18
QMPlusServer/model/autoCodeModel/autoCode.go

@ -2,7 +2,6 @@ package autoCodeModel
import ( import (
"fmt" "fmt"
log "gin-vue-admin/init/initlog"
"html/template" "html/template"
"os" "os"
) )
@ -55,27 +54,28 @@ func Temp() {
ComponentDictionary: nil, ComponentDictionary: nil,
} }
a := AutoCodeStruct{ a := AutoCodeStruct{
StructName: "Test",
PackageName: "autocode",
Components: []Component{a1, a2},
StructName: "Test",
PackageName: "autocode",
Abbreviation: "t",
Components: []Component{a1, a2},
} }
_dir := "../" + a.PackageName _dir := "../" + a.PackageName
exist, err := pathExists(_dir) exist, err := pathExists(_dir)
if err != nil { if err != nil {
log.L.Info("get dir error![%v]\n", err)
//log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
return return
} }
if exist { if exist {
log.L.Info("has dir![%v]\n", _dir)
//log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
} else { } else {
log.L.Info("no dir![%v]\n", _dir)
//log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
// 创建文件夹 // 创建文件夹
err := os.Mkdir(_dir, os.ModePerm) err := os.Mkdir(_dir, os.ModePerm)
if err != nil { if err != nil {
log.L.Error("mkdir failed![%v]\n", err)
//log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
} else { } else {
log.L.Info("mkdir success!\n")
//log.L.Info("mkdir success!\n")
} }
} }
file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755) file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755)

27
QMPlusServer/tpl/te/struct.go.tpl

@ -6,7 +6,6 @@ import (
"gin-vue-admin/init/qmsql" "gin-vue-admin/init/qmsql"
"gin-vue-admin/model/modelInterface" "gin-vue-admin/model/modelInterface"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/pkg/errors"
) )
type {{.StructName}} struct { type {{.StructName}} struct {
@ -15,22 +14,38 @@ type {{.StructName}} struct {
} }
// 创建{{.StructName}} // 创建{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Create{{.StructName}}()(err error){
func ({{.Abbreviation}} *{{.StructName}})Create{{.StructName}}()(err error){
err = qmsql.DEFAULTDB.Create({{.Abbreviation}}).Error err = qmsql.DEFAULTDB.Create({{.Abbreviation}}).Error
return err return err
} }
// 删除{{.StructName}} // 删除{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Delete{{.StructName}}()(err error){
func ({{.Abbreviation}} *{{.StructName}})Delete{{.StructName}}()(err error){
err = qmsql.DEFAULTDB.Delete({{.Abbreviation}}).Error err = qmsql.DEFAULTDB.Delete({{.Abbreviation}}).Error
return err return err
} }
// 更新{{.StructName}} // 更新{{.StructName}}
func ({{.Abbreviation}} *{{StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){
func ({{.Abbreviation}} *{{.StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){
err = qmsql.DEFAULTDB.Save({{.Abbreviation}}).Error
return err, *{{.Abbreviation}}
} }
func ({{.Abbreviation}} *{{StructName}})Create(){
// 根据ID查看单条{{.StructName}}
func ({{.Abbreviation}} *{{.StructName}})FindById()(err error,re{{.Abbreviation}} {{.StructName}}){
err = qmsql.DEFAULTDB.Where("id = ?",{{.Abbreviation}}.ID).First(&re{{.Abbreviation}}).Error
return err,re{{.Abbreviation}}
}
// 分页获取{{.StructName}}
func ({{.Abbreviation}} *{{.StructName}})GetInfoList(info modelInterface.PageInfo)(err error, list interface{}, total int){
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
err, db, total := servers.PagingServer({{.Abbreviation}}, info)
if err != nil {
return
} else {
var re{{.StructName}}List []{{.StructName}}
err = db.Find(&re{{.StructName}}List).Error
return err, re{{.StructName}}List, total
}
} }

3
QMPlusVuePage/src/view/superAdmin/autoCode/index.vue

@ -7,6 +7,9 @@
<el-form-item label="Struct名称" :span="8"> <el-form-item label="Struct名称" :span="8">
<el-input v-model="form.structName"></el-input> <el-input v-model="form.structName"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="Struct简称" :span="8">
<el-input v-model="form.structName"></el-input>
</el-form-item>
<el-form-item label="Package名称" :span="8"> <el-form-item label="Package名称" :span="8">
<el-input v-model="form.packageName"></el-input> <el-input v-model="form.packageName"></el-input>
</el-form-item> </el-form-item>

Loading…
Cancel
Save