发现一个不错的golang框架,文档非常详细,自带代码生产dao工具,可以快速的进行项目开发,还有社区也是非常的活跃

前言


本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/129698017

未经博主允许不得转载。
博主CSDN地址是:https://blog.csdn.net/freewebsys
博主掘金地址是:https://juejin.cn/user/585379920479288
博主知乎地址是:https://www.zhihu.com/people/freewebsystem

1,关于goframe框架介绍

项目地址:https://goframe.org/

GoFrame是一款模块化、低耦合设计的开发框架,包含了常用的基础组件和开发工具,既可以作为完整的业务项目框架使用也可以作为独立的组件库使用。我们为大家提供的快速开始章节,主要以完整的业务项目介绍框架的基本入门和使用。关于独立组件库使用,可以查看独立的组件章节介绍。

github项目地址:

https://github.com/gogf/gf

cd $GOPATH
mkdir -p github.com/gogf/
cd github.com/gogf/
git clone  https://github.com/gogf/gf.git
cd cmd/gf
go install


查看命令行:

gf
USAGE
    gf COMMAND [OPTION]

COMMAND
    up         upgrade GoFrame version/tool to latest one in current project
    env        show current Golang environment variables
    fix        auto fixing codes after upgrading to new GoFrame version
    run        running go codes with hot-compiled-like feature
    gen        automatically generate go files for dao/do/entity/pb/pbentity
    tpl        template parsing and building commands
    init       create and initialize an empty GoFrame project
    pack       packing any file/directory to a resource file, or a go file
    build      cross-building go project for lots of platforms
    docker     build docker image for current GoFrame project
    install    install gf binary to system (might need root/admin permission)
    version    show version information of current binary

OPTION
    -y, --yes       all yes for all command without prompt ask
    -v, --version   show version information of current binary
    -d, --debug     show internal detailed debugging information
    -h, --help      more information about this command

ADDITIONAL
    Use "gf COMMAND -h" for details about a command.
    

然后创建项目:

gf init gf-demo
cd gf-demo 
gf run main.go 

会启动web端口在8000,接口和 api 都有。

2023-03-21 15:28:00.719 [INFO] swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2023-03-21 15:28:00.719 [INFO] openapi specification is serving at address: http://127.0.0.1:8000/api.json

  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |           MIDDLEWARE             
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | gf-demo/internal/controller/hello.(*Controller).Hello           | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------

2,创建Dao代码

工程项目路径:
https://goframe.org/pages/viewpage.action?pageId=30740166

/
├── api
├── hack
├── internal
│   ├── cmd
│   ├── consts
│   ├── controller
│   ├── dao
│   ├── logic
│   ├── model
│   |   ├── do
│   │   └── entity
│   └── service
├── manifest
├── resource
├── utility
├── go.mod
└── main.go 

工具配置好 hack 里面的yaml 然后就可以了:

配置好数据库地址。然后执行生成代码:

make dao
generated: internal/dao/user_info.go
generated: internal/dao/internal/user_info.go
generated: internal/model/do/user_info.go
generated: internal/model/entity/user_info.go
done!

主要是生成了 user_info 的dao 代码:


// UserInfoDao is the data access object for table user_info.
type UserInfoDao struct {
    
    
	table   string          // table is the underlying table name of the DAO.
	group   string          // group is the database configuration group name of current DAO.
	columns UserInfoColumns // columns contains all the column names of Table for convenient usage.
}

// UserInfoColumns defines and stores column names for table user_info.
type UserInfoColumns struct {
    
    
	Id       string // 主键Id
	Name     string // 用户名
	Password string // 密码
	Status   string // 状态
	Type     string // 类型
}

// userInfoColumns holds the columns for table user_info.
var userInfoColumns = UserInfoColumns{
    
    
	Id:       "id",
	Name:     "name",
	Password: "password",
	Status:   "status",
	Type:     "type",
}

// NewUserInfoDao creates and returns a new DAO object for table data access.
func NewUserInfoDao() *UserInfoDao {
    
    
	return &UserInfoDao{
    
    
		group:   "default",
		table:   "user_info",
		columns: userInfoColumns,
	}
}

// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *UserInfoDao) DB() gdb.DB {
    
    
	return g.DB(dao.group)
}

// Table returns the table name of current dao.
func (dao *UserInfoDao) Table() string {
    
    
	return dao.table
}

// Columns returns all column names of current dao.
func (dao *UserInfoDao) Columns() UserInfoColumns {
    
    
	return dao.columns
}

// Group returns the configuration group name of database of current dao.
func (dao *UserInfoDao) Group() string {
    
    
	return dao.group
}

// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *UserInfoDao) Ctx(ctx context.Context) *gdb.Model {
    
    
	return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}

// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *UserInfoDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
    
    
	return dao.Ctx(ctx).Transaction(ctx, f)
}

3,测试Dao的方法

然后就可以生成dao的模板了,再通过controller 进行调用。

参考 orm 的api 文档,简单的测试了下 ,还是挺简单的。

	var (
		m = dao.UserInfo.Ctx(ctx)
	)
	userInfo := entity.UserInfo{
    
    
		Name:     "zhangsan",
		Password: "123456",
		Status:   1,
	}

	lastId, err := m.Data(userInfo).InsertAndGetId()
	if err == nil {
    
    
		g.Log().Printf(ctx, "########## insertId %d #######", lastId)
	}

	count, err := m.Count()
	if err == nil {
    
    
		g.Log().Printf(ctx, "########## count: %d #######", count)
	}

	userList := []entity.UserInfo{
    
    }

	err = m.Scan(&userList)

	g.Log().Printf(ctx, "########## userList: %v #######", userList)

4,总结

goframe框架还是一个非常不错web框架,同时也尽可能的把开发需要的工具。
类库都进行了丰富,也是比较容易上手的。是个全栈MVC框架。
类似的产品是beego,相当于是一个 beego plus 版本。已经又很多人在使用了。
使用的人也是非常的多。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/129698017

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/freewebsys/article/details/129698017
今日推荐