最佳体验请使用Chrome67及以上版本、火狐、Edge、Safari浏览器 ×

创建银行
创建开票

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

    作者:雷建明@芯速配 阅读40 2024/09/29 07:07:13 文章 原创 公开

    目录

    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

              }

         }

    }


    声明:本网站部分内容来源于网络,版权归原权利人所有,其观点不代表本网站立场;本网站视频或图片制作权归当前商户及其作者,涉及未经授权的制作均须标记“样稿”。如内容侵犯了您相关权利,请及时通过邮箱service@ichub.com与我们联系。
     1  1

    微信扫一扫:分享

    微信里点“+”,扫一扫二维码

    便可将本文分享至朋友圈。

      
    
    
    分享
     0
      验证