golang orm

korm

golang orm, 一个简单易用的orm, 支持嵌套事务

安装

go get github.com/wdaglb/korm
go get github.com/go-sql-driver/mysql

支持数据库

连接mysql数据库

err := Connect(Config{
    Driver: "mysql",
    Host:   "127.0.0.1",
    Port:   3306,
    User:   "root",
    Pass:   "",
    Database: "test",
})
if err != nil {
    t.Fatal(err)
}

连接上下文

数据库的读写操作都依托于Context类 Context内部会自动维护db连接,不需要你自行管理Context实例,每次使用都建议实例一个新的Context

ctx := new(Context)

声明模型结构

type Test struct {
	Id int64 `db:"id"`
	User string `db:"user"`
}

查询一行数据

row := &Test{}
if ok, err := ctx.Model(&row).Where("Id", 1).Find(); !ok || err != nil {
    fmt.Println("记录不存在")
    return
}
fmt.Printf("id: %d\n", row.Id)

执行的sql

SELECT * FROM test WHERE id=1

查询多行数据

var rows []Test
if err := ctx.Model(&rows).Select(); err != nil {
    fmt.Println("查询错误")
}
fmt.Printf("rows: %v\n", rows)

执行的sql

SELECT * FROM test

创建数据

insertData := &Test{
  User: "test",
}
if err := ctx.Model(&insertData).Create(); err != nil {
    fmt.Println("创建错误")
}
fmt.Printf("insertData: %v\n", insertData)

执行的sql

INSERT INTO test (`user`) VALUES ('test')

更新数据

updateData := &Test{
  Id: 1,
  User: "test",
}
if err := ctx.Model(&updateData).Update(); err != nil {
    fmt.Println("更新错误")
}
fmt.Printf("updateData: %v\n", updateData)

执行的sql

UPDATE test SET `user`='test' WHERE `id`=1

删除数据

if err := ctx.Model(&Test{Id: 1}).Delete(); err != nil {
    fmt.Println("删除错误")
}
fmt.Printf("删除成功\n")

执行的sql

DELETE FROM test WHERE `id`=1

事务操作

ctx.Transaction(func () error {
    // 事务逻辑代码
})

只需要把需要进行的事务,写到闭包函数里即可,支持嵌套事务 注意:在同一个Context实例里的才会被事务影响

License

@ King east, 2021-now

Released under the MIT License

Similar Resources

ORM-ish library for Go

We've moved! gorp is now officially maintained at: https://github.com/go-gorp/gorp This fork was created when the project was moved, and is provided f

Aug 23, 2022

Simple Go ORM for Google/Firebase Cloud Firestore

go-firestorm Go ORM (Object-relational mapping) for Google Cloud Firestore. Goals Easy to use Non intrusive Non exclusive Fast Features Basic CRUD ope

Dec 1, 2022

Database agnostic ORM for Go

If you are looking for something more lightweight and flexible, have a look at jet For questions, suggestions and general topics visit the group. Inde

Nov 28, 2022

QBS stands for Query By Struct. A Go ORM.

Qbs Qbs stands for Query By Struct. A Go ORM. 中文版 README ChangeLog 2013.03.14: index name has changed to {table name}_{column name}. For existing appl

Sep 9, 2022

Generate a Go ORM tailored to your database schema.

Generate a Go ORM tailored to your database schema.

SQLBoiler is a tool to generate a Go ORM tailored to your database schema. It is a "database-first" ORM as opposed to "code-first" (like gorm/gorp). T

Jan 2, 2023

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm

xorm HAS BEEN MOVED TO https://gitea.com/xorm/xorm . THIS REPOSITORY WILL NOT BE UPDATED ANY MORE. 中文 Xorm is a simple and powerful ORM for Go. Featur

Jan 3, 2023

A better ORM for Go, based on non-empty interfaces and code generation.

reform A better ORM for Go and database/sql. It uses non-empty interfaces, code generation (go generate), and initialization-time reflection as oppose

Dec 31, 2022

A better ORM for Go, based on non-empty interfaces and code generation.

A better ORM for Go, based on non-empty interfaces and code generation.

A better ORM for Go and database/sql. It uses non-empty interfaces, code generation (go generate), and initialization-time reflection as opposed to interface{}, type system sidestepping, and runtime reflection. It will be kept simple.

Dec 29, 2022

Simple and performant ORM for sql.DB

Simple and performant ORM for sql.DB Main features are: Works with PostgreSQL, MySQL, SQLite. Selecting into a map, struct, slice of maps/structs/vars

Jan 4, 2023
Related tags
100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood.

go-queryset 100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. Contents

Dec 30, 2022
An orm library support nGQL for Golang

norm An ORM library support nGQL for Golang. Overview Build insert nGQL by struct / map (Support vertex, edge). Parse Nebula execute result to struct

Dec 1, 2022
golang orm

korm golang orm, 一个简单易用的orm, 支持嵌套事务 安装 go get github.com/wdaglb/korm go get github.com/go-sql-driver/mysql 支持数据库 mysql https://github.com/go-sql-driv

Oct 31, 2022
Golang ORM with focus on PostgreSQL features and performance

go-pg is in a maintenance mode and only critical issues are addressed. New development happens in Bun repo which offers similar functionality but works with PostgreSQL, MySQL, and SQLite.

Jan 8, 2023
The fantastic ORM library for Golang, aims to be developer friendly

GORM The fantastic ORM library for Golang, aims to be developer friendly. Overview Full-Featured ORM Associations (Has One, Has Many, Belongs To, Many

Nov 11, 2021
SQL mapper ORM framework for Golang
 SQL mapper ORM framework for Golang

SQL mapper ORM framework for Golang English 中文 Please read the documentation website carefully when using the tutorial. DOC Powerful Features High Per

Dec 8, 2021
Golang mysql orm, a personal learning project, dedicated to easy use of mysql

golang mysql orm 个人学习项目, 一个易于使用的mysql-orm mapping struct to mysql table golang结构

Dec 30, 2021
Mybatis for golang - SQL mapper ORM framework

SQL mapper ORM framework for Golang English 中文 Please read the documentation website carefully when using the tutorial. DOC Powerful Features High Per

Nov 10, 2022
beedb is a go ORM,support database/sql interface,pq/mysql/sqlite

Beedb ❗ IMPORTANT: Beedb is being deprecated in favor of Beego.orm ❗ Beedb is an ORM for Go. It lets you map Go structs to tables in a database. It's

Nov 25, 2022
A simple wrapper around sql.DB to help with structs. Not quite an ORM.

go-modeldb A simple wrapper around sql.DB to help with structs. Not quite an ORM. Philosophy: Don't make an ORM Example: // Setup require "modeldb" db

Nov 16, 2019