GO自动代码工具--一键生成神器

目录

GO自动代码工具--一键生成神器

1. 实现方法:

2. UML:

3. 实现的功能接口

4. 实施条件:

4.1. 目前已经测试过,较成熟可应用。

4.2. 实施效果,通过泛型简化代码,提升效率。

4.3. 昨天讨论的微服务拆分暂定通用域

5. GIT:

6. 使用方法

7. 支持视图:相当于支持多表关联

8. 代码简介:

8.1. 代码模板文件

8.2. 基类type BaseModel struct {

8.3. 实体类

8.4. DAO类

8.5. SERVICE类

8.6. 测试代码

8.7. 测试结果

GO自动代码工具--一键生成神器

2024年9月3日星期二

  1. 实现方法:

对于表的增删改查没有业务逻辑,可以通过代码生成。

查询数据库的表字典,转换成GO的类型信息,利用预先配置的代码模板,

生成具体的代码和测试代码。

  1. UML

  1. 实现的功能接口

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
}

  1. 实施条件:
    1. 目前已经测试过,较成熟可应用。
    2. 实施效果,通过泛型简化代码,提升效率。
    3. 昨天讨论的微服务拆分暂定通用域
    4. 但webcli120的能力都可以使用:esserver对接, gocode 代码工具生成的代码等。

  1. GIT:

http://git.ichub.com/general/gocode.git

依赖于webcli120工程,webcli120无业务逻辑代码。

  1. 使用方法

go get git.ichub.com/general/gocode
go install  git.ichub.com/general/gocode/cmd/gocode

执行命令

gocode cockdb service_menu_template

  1. 支持视图:相当于支持多表关联

 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()

}

  1. 代码简介
    1. 代码模板文件

    1. 基类type BaseModel struct {


    //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:"-"`
}

    1. 实体类

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"`

...

}

    1. DAO类

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()

}

    1. SERVICE类

// 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

    1. 测试代码

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))
}

    1. 测试结果

 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

          }

     }

}

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值