dbstruct

package module
v0.0.0-...-0d04a05 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 14 Imported by: 0

README

介绍

dbstruct是一款将数据库表一键转换为Golang Struct的应用程序,支持自定义Tag和多种命名格式配置。

参数列表

dsn              string   //数据库链接
tables           []string //自定义表
tagJson          bool     //json tag
tagOrm           bool     //orm tag
fieldNameFmt     FmtMode  //字段名称格式
structNameFmt    FmtMode  //结构名格式
fileNameFmt      FmtMode  //文件名格式
genTableName     string   //TableName方法名,
genTableNameFunc bool     //是否生成TableName方法
modelPath        string   //model保存的路径,若singleFile==true,则填写model.go的完整路径,默认为当前路径
singleFile       bool     //是否合成一个单文件
packageName      string   //包名
tags             []*Tag   //自定义Tag列表

使用方法

安装依赖

go get github.com/Licoy/dbstruct

使用

err := dbstruct.NewDBStruct().
    Dsn("root:root@tcp(127.0.0.1:3306)/dbname?charset=utf8").
    StructNameFmt(dbstruct.FmtUnderlineToStartUpHump).
    FieldNameFmt(dbstruct.FmtUnderlineToStartUpHump).
    FileNameFmt(dbstruct.FmtUnderline).
    //SingleFile(true).
    GenTableNameFunc(true).
    GenTableName("TableName").
    TagJson(true).
    PackageName("model").
    TagOrm(true).
    AppendTag(dbstruct.NewTag("xml", dbstruct.FmtDefault)).
    Generate()
if err != nil {
    fmt.Println(err)
}

生成结果示例

package model

type Mart struct {
	Id         int64 `xml:"id" json:"id" orm:"id" `
	ActId      int64 `xml:"act_id" json:"act_id" orm:"act_id" `
	Created    int64 `xml:"created" json:"created" orm:"created" `
	TemplateId int   `xml:"template_id" json:"template_id" orm:"template_id" `
	Number     int   `xml:"number" json:"number" orm:"number" `
	Price      int   `xml:"price" json:"price" orm:"price" `
	Day        int   `xml:"day" json:"day" orm:"day" `
}

func (m *Mart) MyTableName() string {
	return "mart"
}

授权

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDBStruct

func NewDBStruct() *dbStruct

Types

type FmtMode

type FmtMode uint16
const (
	FmtDefault                 FmtMode = iota //默认(和表名一致)
	FmtUnderlineToStartUpHump                 //下划线转开头大写驼峰
	FmtUnderlineToStartLowHump                //下划线开头小写驼峰
	FmtUnderline                              //下划线格式
)

type Tag

type Tag struct {
	TagName string
	Mode    FmtMode
}

func NewTag

func NewTag(tagName string, mode FmtMode) *Tag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL