Go-mysql-orm - Golang mysql orm,dedicated to easy use of mysql

Overview

golang mysql orm

个人学习项目, 一个易于使用的mysql-orm

mapping struct to mysql table

golang结构体映射mysql table,以便于后续操作表

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "github.com/folospace/mysql-go-orm/orm"
)

//mysql db
var db, _ = sql.Open("mysql", "user:[email protected](127.0.0.1:3306)/mydb?parseTime=true&charset=utf8mb4&loc=Asia%2FShanghai")

//user table model
var UserTable = new(User)

type User struct {
    Id   int    `json:"id"`
    Name string `json:"name"`
}

func (m *User) Query() *orm.Query {
    return new(orm.Query).UseDB(db).FromTable(m)
}
func (*User) TableName() string {
    return "user"
}
func (*User) DatabaseName() string {
    return "mydb"
}

select query

查询table数据, 填充到各种数据类型中

func main() {
    {
        var data User //select one user
        UserTable.Query().Limit(1).Select(&data)
        fmt.Println(data)
    }
    {
        var data []User //select users
        UserTable.Query().Limit(5).Select(&data)
        fmt.Println(data)
    }
    {
        var data int //select count
        UserTable.Query().Limit(1).SelectCount(&data)
        fmt.Println(data)
    }
    {
        var data []int //select user.ids
        UserTable.Query().Limit(5).Select(&data)
        fmt.Println(data)
    }
    {
        var data map[int]User //select map[id]User
        UserTable.Query().Limit(5).Select(&data)
        fmt.Println(data)
    }
    {
        var data map[int]string //select map[id]name
        UserTable.Query().Limit(5).Select(&data)
        fmt.Println(data)
    }
}

update query

更新表数据

    {
        //update user set name="hello" where id=1
        UserTable.Query().Where(&UserTable.Id, 1).Update(&UserTable.Name, "hello")
    }

join and where

连表和where条件

       {
           //update user join order on user.id=order.user_id 
           //set order.order_amount=100
           //where user.id in (1,2)
           UserTable.Query().Join(OrderTable, func(query *orm.Query) {
               query.Where(&UserTable.Id, &OrderTable.UserId)
           }).
               Where(&UserTable.Id, orm.WhereIn, []int{1,2}). 
               Update(&OrderTable.OrderAmount, 100)
       }
You might also like...
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

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

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

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

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

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

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

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.

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

Releases(v0.7.1)
Owner
magacy
magacy
Using-orm-with-db - Trying to use ORM to work with PostgreSQL

Using ORM with db This repo contains the training (rough) code, and possibly not

Pavel V 5 Jul 31, 2022
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

Denis Isaev 692 Dec 30, 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

astaxie 710 Nov 25, 2022
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

null 6.6k Jan 3, 2023
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

Zhihu 77 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

null 6 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.

null 5.3k 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

Yadhu Sasidharan 0 Nov 11, 2021
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

null 0 Dec 8, 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

Tim 3 Nov 10, 2022