golang的orm框架xorm(一)--- xorm 安装及简单使用

简介

xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。xorm的目标并不是让你完全不去学习SQL,我们认为SQL并不会为ORM所替代,但是ORM将可以解决绝大部分的简单SQL需求。xorm支持两种风格的混用。

特性

支持Struct和数据库表之间的灵活映射,并支持自动同步

  • 事务支持
  • 同时支持原始SQL语句和ORM操作的混合执行
  • 使用连写来简化调用
  • 支持使用Id, In, Where, Limit, Join, Having, Table, SQL,Cols等函数和结构体等方式作为条件
  • 支持级联加载Struct
  • Schema支持(仅Postgres)
  • 支持缓存
  • 支持根据数据库自动生成xorm的结构体
  • 支持记录版本(即乐观锁)
  • 内置SQL Builder支持
  • 通过EngineGroup支持读写分离和负载均衡

驱动支持

xorm当前支持的驱动和数据库如下:

  • Mysql: github.com/go-sql-driver/mysql
  • MyMysql:github.com/ziutek/mymysql/godrv
  • Postgres: github.com/lib/pq
  • Tidb:github.com/pingcap/tidb
  • SQLite: github.com/mattn/go-sqlite3
  • MsSql:github.com/denisenkom/go-mssqldb
  • MsSql: github.com/lunny/godbc
  • Oracle: github.com/mattn/go-oci8 (试验性支持)
  • ql: github.com/cznic/ql(试验性支持)

安装

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)
}

你可能感兴趣的:(go学习,golang,数据库,mysql)