GO自动代码工具--一键生成神器
目录
8.2. 基类type BaseModel struct {
GO自动代码工具--一键生成神器
2024年9月3日星期二
对于表的增删改查没有业务逻辑,可以通过代码生成。
查询数据库的表字典,转换成GO的类型信息,利用预先配置的代码模板,
生成具体的代码和测试代码。
编辑
type GeneralDaoIface[P int32 | int64 | string, E IBaseModel[P]] interface {
Insert(entity E) (P, error)
DeleteById(pkey P) error
Save(entity E) (P, error)
Update(entity E) (P, error)
UpdateNotNull(pkey P, maps map[string]any) (P, error)
UpdateMap(pkey P, maps map[string]interface{}) (P, error)
FindById(pkey P) (entity E, found bool, err error)
FindByIds(pks string) (*[]E, error)
QueryPageui() *pageui.PageuiResult
Query() *page.PageResult
QueryModel() *pagemodel.PageResult[E]
Count() (int, error)
GetDB() *gorm.DB
}
http://git.ichub.com/general/gocode.git
依赖于webcli120工程,webcli120无业务逻辑代码。
go get git.ichub.com/general/gocode
go install git.ichub.com/general/gocode/cmd/gocode
执行命令
gocode cockdb service_menu_template
编辑
gocode cockdb v_account
type VAccount struct {
basedto.BaseEntity `gorm:"-"`
*basemodel.BaseModel
/* */
。。。
}
编辑
import (
"git.ichub.com/general/webcli120/goconfig/ichubcontext"
"git.ichub.com/general/webcli120/goweb/generaldb/generaldao"
"git.ichub.com/general/webcli120/goconfig/base/basedto"
"git.ichub.com/general/webcli120/code/postgres/db/model"
"github.com/jinzhu/gorm"
)
// var InstVAccountDao = NewVAccountDao()
type VAccountDao struct {
basedto.BaseEntity `gorm:"-"`
*generaldao.BaseDao[int64, * model.VAccount]
}
func NewVAccountDao() *VAccountDao {
var dao = &VAccountDao{
BaseDao: generaldao.NewBaseDao[int64, * model.VAccount](),
}
dao.InitProxy(dao)
return dao
}
func (this *VAccountDao) GetDB() *gorm.DB {
return ichubcontext.FindBeanIchubClientFactroy().GetDB()
}
编辑
//Id string `json:"id"`
// 时间类型
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
//ui used 时间整型
CreatedAtInt int64 `json:"created_at_int" gorm:"-"`
UpdatedAtInt int64 `json:"updated_at_int" gorm:"-"`
DeleteAtInt *int64 `json:"deleted_at_int" gorm:"-"`
// 创建与更新者
CreatedBy int64 `json:"created_by"`
UpdatedBy int64 `json:"updated_by"`
DeletedBy int64 `json:"deleted_by"`
// biz set2ui 创建与更新人名称
CreatedByName string `json:"created_by_name" gorm:"-"`
UpdatedByName string `json:"updated_by_name" gorm:"-"`
DeletedByName string `json:"deleted_by_name" gorm:"-"`
}
type ServiceMenuTemplate struct {
basedto.BaseEntity `gorm:"-"`
*basemodel.BaseModel
/* */
Id int64 `gorm:"column:id;type:INT8;PRIMARY_KEY;default:unique_rowid()" json:"id"`
/* */
WebsiteTemplateId int64 `gorm:"column:website_template_id;type:INT8" json:"website_template_id"`
...
}
编辑
type ServiceMenuTemplateDao struct {
basedto.BaseEntity `gorm:"-"`
*generaldao.BaseDao[int64, * model.ServiceMenuTemplate]
}
func NewServiceMenuTemplateDao() *ServiceMenuTemplateDao {
var dao = &ServiceMenuTemplateDao{
BaseDao: generaldao.NewBaseDao[int64, * model.ServiceMenuTemplate](),
}
dao.InitProxy(dao)
return dao
}
func (this *ServiceMenuTemplateDao) GetDB() *gorm.DB {
return ichubcontext.FindBeanIchubClientFactroy().GetDB()
}
// SERVICE层服务结构体
type ServiceMenuTemplateService struct {
basedto.BaseEntity `gorm:"-"`
// user by testing
* dao.ServiceMenuTemplateDao
}
func NewServiceMenuTemplateService() *ServiceMenuTemplateService {
var s = &ServiceMenuTemplateService{
ServiceMenuTemplateDao : dao.NewServiceMenuTemplateDao(),
}
s.InitProxy(s)
return s
}
// used by real
func (this *ServiceMenuTemplateService) FindDao() * dao.ServiceMenuTemplateDao {
return dao.NewServiceMenuTemplateDao()
}
// End of Service
编辑
func (this *TestServiceMenuTemplateDaoSuite) Test006_QueryView() {
logrus.Info("start Test006_QueryView ...")
this.Dao.PageRequest.PageSize = 3
var result = this.Dao.QueryModel()
logrus.Info(result)
this.Equal(200, result.Code)
logrus.Info(pageui.FromPageModel(result))
}
git.ichub.com/general/webcli120/code/postgres/test/dao.(*TestServiceMenuTemplateDaoSuite).Test006_QueryView() {
"code": 200,
"msg": "成功",
"data": {
"list": [
{
"created_at": "2024-08-29T07:16:09.072218Z",
"updated_at": "2024-09-03T08:41:47.679757Z",
"deleted_at": null,
"created_at_int": 1724915769072,
"updated_at_int": 1725352907679,
。。。
"page_router": "/portal/sku",
"page_perms": "",
"page_path": "./pages/portal/qa_line",
"page_template_id": 0,
"active": true
},
{
"created_at": "2024-08-29T08:05:10.854621Z",
"updated_at": "2024-09-03T08:41:48.76467Z",
"deleted_at": null,
。。。
"navigate_type": 20,
"page_router": "/portal/shop",
"page_perms": "",
"page_path": "./pages/portal/shop",
"page_template_id": 0,
"active": true
},
{
"created_at": "2024-08-29T08:36:55.537558Z",
"updated_at": "2024-09-03T09:36:29.75866Z",
。。。
"description": "bbb",
"page_router": "/portal/members",
"page_perms": "",
"page_path": "./pages/portal/member",
"page_template_id": 0,
"active": true
}
],
"pagination": {
"total": 20,
"current": 1,
"page_size": 3
}
}
}