xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。xorm的目标并不是让你完全不去学习SQL,我们认为SQL并不会为ORM所替代,但是ORM将可以解决绝大部分的简单SQL需求。xorm支持两种风格的混用。
支持Struct和数据库表之间的灵活映射,并支持自动同步
xorm当前支持的驱动和数据库如下:
go get xorm.io/xorm
要想使用xorm要有mysql的驱动
go get -u github.com/go-sql-driver/mysql
定义一个结构体
package modle
import "time"
type User struct {
Id int64
Name string
Age int
Password string `xorm:"varchar(255)"`
Created time.Time `xorm:"created"` // 创建的时候自动创建
Updated time.Time `xorm:"updated"` // 更新的时候自动更新
}
main
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
"xorm/modle"
)
func link() {
//数据库连接基本信息
var (
userName string = "root"
password string = "mysql729"
ipAddress string = "127.0.0.1"
port int = 3306
dbName string = "go_test"
charset string = "utf8mb4"
)
//构建数据库连接
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset:%s", userName, password, ipAddress, port, dbName, charset)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败")
}
err = engine.Sync(new(modle.User))
if err != nil {
fmt.Println("表结构同步失败")
}
}
package main
import (
"fmt"
"xorm.io/xorm"
"xorm/modle"
)
func main() {
//数据库连接基本信息
var (
userName string = "root"
password string = "mysql729"
ipAddress string = "127.0.0.1"
port int = 3306
dbName string = "go_test"
charset string = "utf8mb4"
)
//构建数据库连接
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset:%s", userName, password, ipAddress, port, dbName, charset)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败")
}
//查询
results, err := engine.Query("select * from user")
fmt.Println(results)
result2, err := engine.QueryString("select * from user")
fmt.Println(result2)
result3, err := engine.QueryInterface("select * from user")
fmt.Println(result3)
//Get
user := modle.User{}
engine.Get(&user) // 无条件查询第一个数据
fmt.Println(user)
// 指定条件来查询用户
user1 := modle.User{}
engine.Where("name=?", "user").Desc("id")
}
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
)
func update() {
//数据库连接基本信息
var (
userName string = "root"
password string = "mysql729"
ipAddress string = "127.0.0.1"
port int = 3306
dbName string = "go_test"
charset string = "utf8mb4"
)
//构建数据库连接
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset:%s", userName, password, ipAddress, port, dbName, charset)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败")
}
//user := modle.User{Name: "jay chou"}
//n, _ := engine.ID(1000).Update(&user)
//fmt.Println(n)
engine.Exec("update user set age = ? where id = ?", 10, 10001)
}
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
"xorm/modle"
)
func insert() {
//数据库连接基本信息
var (
userName string = "root"
password string = "mysql729"
ipAddress string = "127.0.0.1"
port int = 3306
dbName string = "go_test"
charset string = "utf8mb4"
)
//构建数据库连接
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset:%s", userName, password, ipAddress, port, dbName, charset)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败")
}
//engine.InsertI() 插入对象,返回值:受影响的行数
user := modle.User{Id: 1000, Name: "user", Age: 18, Password: "hhh"}
n, _ := engine.Insert(&user)
if n > 1 {
fmt.Println("数据插入成功")
}
fmt.Println(n)
user1 := modle.User{Id: 10001, Name: "user", Age: 18, Password: "hhh"}
user2 := modle.User{Id: 10002, Name: "user", Age: 18, Password: "hhh"}
n, _ = engine.Insert(&user1, &user2)
if n > 1 {
fmt.Println("数据插入成功")
}
//切片插入
var users []modle.User
users = append(users, modle.User{Id: 10003, Name: "user", Age: 18, Password: "hhh"})
users = append(users, modle.User{Id: 10004, Name: "user", Age: 18, Password: "hhh"})
n, _ = engine.Insert(&users)
if n > 1 {
fmt.Println("数据插入成功")
}
}
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
"xorm/modle"
)
func del() {
//数据库连接基本信息
var (
userName string = "root"
password string = "mysql729"
ipAddress string = "127.0.0.1"
port int = 3306
dbName string = "go_test"
charset string = "utf8mb4"
)
//构建数据库连接
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset:%s", userName, password, ipAddress, port, dbName, charset)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败")
}
user := modle.User{Name: "jay chou"}
n, _ := engine.ID(1000).Delete(&user)
fmt.Println(n)
}