接上期文章 go goframe2.0 + vue 开源工程的环境搭建_qq_31683775的博客-CSDN博客
安装 gf Releases · gogf/gf-cli · GitHub
安装路径设置为 c:\Windows
配置工程自动生成代码的数据库连接
新建测试表
在工程根目录打开cmd小执行 gf
gf gen dao -t hg_test
已经生成了文件
接下来写剩余的代码
package adminForm
import (
"github.com/bufanyun/hotgo/app/form/input"
"github.com/gogf/gf/v2/frame/g"
)
// 查询列表
type TestListReq struct {
Name string `json:"name" dc:"部门名称"`
g.Meta `path:"/test/list" method:"get" tags:"部门" summary:"获取部门列表"`
}
type TestRes []*input.AdminTestListModel
package input
// 获取列表树
type AdminTestListModel struct {
Name string
}
// 获取列表
type AdminTestListInp struct {
Name string
}
package adminService
import (
"context"
"github.com/bufanyun/hotgo/app/consts"
"github.com/bufanyun/hotgo/app/form/input"
"github.com/bufanyun/hotgo/app/service/internal/dao"
"github.com/gogf/gf/v2/errors/gerror"
)
var Test = new(test)
type test struct{}
func (service *test) List(ctx context.Context, in input.AdminTestListInp) (list []*input.AdminTestListModel, err error) {
mod := dao.Test.Ctx(ctx)
var (
)
// 部门名称
if in.Name != "" {
}
err = mod.Order("id desc").Scan(&list)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return list, err
}
return list, err
}
package adminController
import (
"context"
"github.com/bufanyun/hotgo/app/form/adminForm"
"github.com/gogf/gf/v2/util/gconv"
"github.com/bufanyun/hotgo/app/form/input"
"github.com/bufanyun/hotgo/app/service/adminService"
)
var Test= test{}
type test struct{}
func (controller *test) List(ctx context.Context, req *adminForm.TestListReq) (*adminForm.TestRes, error) {
var (
in input.AdminTestListInp
res adminForm.TestRes
)
if err := gconv.Scan(req, &in); err != nil {
return nil, err
}
data, err := adminService.Test.List(ctx, in)
if err != nil {
return nil, err
}
_ = gconv.Structs(data, &res)
return &res, nil
}
测试接口
更多推荐