• golangmodel 层代码自动生成工具


    一、安装

    1.开启go mod模式: 
    $ export GO111MODULE=on GOPROXY=https://goproxy.cn

    2.引入包
    package main

    import (
    _ "github.com/1819997197/ws-tools"
    )

    3.生成mod文件
    $ go mod init tools

    4.安装
    $ go install github.com/1819997197/ws-tools
    安装成功后,会在$GOBIN目录生成一个二进制文件(ws-tools)

    二、工具使用

    1.查看使用帮助

    $ ws-tools sql --help
    Table structure auto generation model

    Usage:
    sql [flags]

    Flags:
    --conn string   数据库连接dsn user:pwd@tcp(ip:port)/table?charset=utf8&parseTime=true
    --dist string   model层代码生产目录 (default "./models")
    -h, --help           help for sql
    --pkg string     生成的代码与src的相对路径 (default "models")
    --table string   所需生成的表,用逗号分割(默认导出所有的表)

    2.生成model文件

    $ ws-tools sql --conn="user:pwd@tcp(ip:port)/db?charset=utf8&parseTime=true"
    // 默认会在执行命令的当前目录的models目录下生成model文件(models目录需要存在)

    3.自动生成的文件代码

    // interface {{models}}需要修改为具体对应的目录
    business_api.go 文件生成的代码如下:

    package models

    import (
    "git.wondershare.cn/DCStudio/chaos_go/core/database"
    "github.com/jinzhu/gorm"
    "time"
    )

    //go:generate mockgen -destination=./mock/business_api.go -package=mock {{models}} BusinessApiRepositoryIFace
    type BusinessApiRepositoryIFace interface {
    Create(data *BusinessApiModel) error
    TxCreate(tx *gorm.DB, data *BusinessApiModel) error
    Save(data *BusinessApiModel) error
    TxSave(tx *gorm.DB, data *BusinessApiModel) error
    UpdateFields(where, updateFiles map[string]interface{}) error
    }

    // BusinessApiModel 针对数据库表 business_api 的结构体定义
    type BusinessApiModel struct {
    Id uint32 `json:"id"` // Id 类型: uint32 主健字段(Primary Key) 自增长字段
    BusiId uint32 `json:"busi_id"` // BusiId 类型: uint32 默认值: 0
    Type uint8 `json:"type"` // 1-预授权 类型: uint8 说明: 4-取消续费 默认值: 0
    Url string `json:"url"` // Url 类型: string
    AuthSetting string `json:"auth_setting"` // 接口验证配置json 类型: string 说明: 默认basic
    CreateTime time.Time `json:"create_time"` // CreateTime 类型: time.Time 默认值: CURRENT_TIMESTAMP
    UpdateTime time.Time `json:"update_time"` // UpdateTime 类型: time.Time 默认值: CURRENT_TIMESTAMP
    }

    func (BusinessApiModel) TableName() string {
    return "business_api"
    }

    type BusinessApiEntity struct {
    db *database.DB
    }

    func NewBusinessApiEntity(db *database.DB) BusinessApiRepositoryIFace {
    return &BusinessApiEntity{db}
    }

    // getListBy 根据条件查找列表
    func (entity *BusinessApiEntity) getListBy(fields string, where map[string]interface{}) ([]*BusinessApiModel, error) {
    var list []*BusinessApiModel
    query := entity.db.Where(where)
    if fields != "" {
    query = query.Select(fields)
    } else {
    query = query.Select(`id`, `busi_id`, `type`, `url`, `auth_setting`, `create_time`, `update_time`)
    }
    err := query.Find(&list).Error
    if err == gorm.ErrRecordNotFound {
    return nil, nil
    }
    if err != nil {
    return nil, err
    }
    return list, nil
    }

    // getBy 根据条件查找一条记录
    func (entity *BusinessApiEntity) getBy(fields string, where map[string]interface{}) (*BusinessApiModel, error) {
    var model = &BusinessApiModel{}
    query := entity.db.Where(where)
    if fields != "" {
    query = query.Select(fields)
    } else {
    query = query.Select(`id`, `busi_id`, `type`, `url`, `auth_setting`, `create_time`, `update_time`)
    }
    err := query.First(model).Error
    if err == gorm.ErrRecordNotFound {
    return nil, nil
    }
    if err != nil {
    return nil, err
    }
    return model, nil
    }

    // Create 写入记录
    func (entity *BusinessApiEntity) Create(data *BusinessApiModel) error {
    return entity.db.Create(data).Error
    }

    // TxCreate 写入记录
    func (entity *BusinessApiEntity) TxCreate(tx *gorm.DB, data *BusinessApiModel) error {
    return tx.Create(data).Error
    }

    // Save 更新记录
    func (entity *BusinessApiEntity) Save(data *BusinessApiModel) error {
    return entity.db.Save(data).Error
    }

    // TxSave 更新记录
    func (entity *BusinessApiEntity) TxSave(tx *gorm.DB, data *BusinessApiModel) error {
    return tx.Save(data).Error
    }

    // UpdateFields 根据条件更新记录
    func (entity *BusinessApiEntity) UpdateFields(where, updateFiles map[string]interface{}) error {
    return entity.db.Where(where).UpdateColumns(updateFiles).Error
    }
  • 相关阅读:
    According to TLD, tag fmt:formatDate must be empty, but is not 问题的解决
    README.md 编写
    查阅Springboot官方文档方式----------------Springboot2.0.2最新稳定版
    IntelliJ IDEA 2017版 Spring5 java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/Object;)V
    IntelliJ IDEA 2017版 SpringBoot测试类编写
    IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置
    fastjson 错误解决方案详情 com.alibaba.fastjson.JSONException: syntax error, expect {, actual EOF, pos 1410
    详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.
    Netty学习第六节实例一步学习
    Netty学习第五节实例进一步学习
  • 原文地址:https://www.cnblogs.com/jiangzuo/p/15543898.html
Copyright © 2020-2023  润新知