推荐一款Go ORM Framework, 纯手工打造

介绍笔者自行开发了一款Go ORM Framework,欢迎大家交流学习。

介绍

笔者自行开发了一款Go ORM Framework,欢迎大家交流学习。

项目地址:

https://github.com/billcoding/gorm

创建数据库和表

  • 创建数据库
create database gorm_db;
  • 创建人员表
create table person(    id      int         not null auto_increment comment 'ID',    name    varchar(50) not null comment '名称',    age     int         not null comment '年龄',    mobile  varchar(11) not null comment '手机号码',    address varchar(50) not null comment '地址',    primary key (id),    unique index u_idx_name (name)) comment '人员表';

推荐一款Go ORM Framework, 纯手工打造

数据库和表

使用gocode-generator生成模型源代码


$ go install github.com/billcoding/gocode-generator@latest
  • gocode-generator的用法
$ gocode-generator gen -hGenerate Go files.Simply type gocode-generator help gen for full details.Usage:  gocode-generator gen [flags]Aliases:  gen, g, generateExamples:gocode-generator -D "root:123@tcp(127.0.0.1:3306)/test" -d "database" -M "awesome"gocode-generator -D "root:123@tcp(127.0.0.1:3306)/test" -d "database" -M "awesome" -o "/to/path"gocode-generator -D "root:123@tcp(127.0.0.1:3306)/test" -d "database" -M "awesome" -au "bigboss" -o "/to/path"
  • 生成模板源代码
$ gocode-generator gen -D "root:passwd@tcp(localhost:3306)/gorm_db" -d "gorm_db" -M "awesomeProject" --mapper=F -V

推荐一款Go ORM Framework, 纯手工打造

config/config.go

推荐一款Go ORM Framework, 纯手工打造

model/person.go

新增(Create)

package mainimport (	"awesomeProject/model"	"fmt"	"github.com/billcoding/gorm")func main() {	orm := gorm.New(new(model.Person))	err := orm.Insert().Save(&model.Person{		Name:    "apple",		Age:     20,		Mobile:  "13911112222",		Address: "china",	})	if err != nil {		fmt.Println(err)	} else {		fmt.Println("保存成功")	}}

删除(Delete)

package mainimport (	"awesomeProject/model"	"fmt"	"github.com/billcoding/gorm")func main() {	orm := gorm.New(new(model.Person))	err := orm.Delete().Remove(&model.Person{ID: 3})	if err != nil {		fmt.Println(err)	} else {		fmt.Println("删除成功")	}}

修改(Update)

package mainimport (	"awesomeProject/model"	"fmt"	"github.com/billcoding/gorm")func main() {	orm := gorm.New(new(model.Person))	err := orm.Update().Modify(&model.Person{ID: 1, Name: "orange", Mobile: "19811112222", Age: 35, Address: "China WH"})	if err != nil {		fmt.Println(err)	} else {		fmt.Println("修改成功")	}}

查询(Retrieve)

package mainimport (	"awesomeProject/model"	"fmt"	"github.com/billcoding/gorm")func main() {	orm := gorm.New(new(model.Person))	person, err := orm.Select().First(nil)	if err != nil {		fmt.Println(err)	}	fmt.Println(person)}

配置

  • 配置数据源
// 配置master数据源gorm.DS(DSN)// 配置指定名称的数据源gorm.DSWithName("secodary", DSN)
  • 配置自动创建表
gorm.Configuration.Migrate = true
  • 配置钩子
// Insertgorm.Configuration.InsertHookers = append(gorm.Configuration.InsertHookers, insertHookers...)// Updategorm.Configuration.UpdateHookers = append(gorm.Configuration.UpdateHookers, updateHookers...)// Deletegorm.Configuration.DeleteHookers = append(gorm.Configuration.DeleteHookers, deleteHookers...)// Selectgorm.Configuration.SelectHookers = append(gorm.Configuration.SelectHookers, selectHookers...)
  • ModelMeta
// Migrate defines migrate create or update table in the databaseMigrate bool// Table defines table name for the Model// If Table is empty, set from Configuration's TableNameStrategyTable string// Comment defines table comment for the ModelComment string// Table defines column name Strategy for the Model's Fields// see strategy.Default, strategy.Underline, strategy.CamelCase,ColumnNameStrategy Strategy// DB defines DS name from dsMap, default: `_` or `master`DS string// PrimaryKeyColumns defines Table primary key columnsPrimaryKeyColumns []sgen.C// ColumnDefinitions defines Table column definitions// Use sgen.ColumnDefinition to buildColumnDefinitions []sgen.Ge// Indexes defines Table index definitions// Use sgen.IndexDefinition to buildIndexDefinitions []sgen.Ge// InsertIgnores defines Table insert ignore columnsInsertIgnores []sgen.C// UpdateIgnores defines Table update ignore columnsUpdateIgnores []sgen.C

鸣谢(Thanks)

非常感谢JetBrains对开源项目的支持!

https://jb.gg/OpenSource

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/75588.html

(0)

相关推荐