简介:
项目地址:github.com/go-xorm/xorm
支持sql 也有orm结构支持
demo
直接上代码,需要注意的内容都打了注释,包管理不再赘述。
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql" // 引入没有具体用 所以前面加个_ 来表示只引入init
"github.com/go-xorm/xorm"
"log"
)
const DriverName = "mysql"
const MasterDataSourceName = "root:2123wqwe@tcp(127.0.0.1:3306)/learn_go?charset=utf8"
//CREATE TABLE `people` (
//`id` int(11) NOT NULL,
//`age` int(11) DEFAULT NULL,
//`name` varchar(45) DEFAULT NULL,
//PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='people table'
//这里有个go的规则,struct中大写变量是public 小写是private ,考虑到后面要读写,需要为大写。
type People struct {
Id int `xorm:"not null pk autoincr"`
Age int
Name string
}
var engine *xorm.Engine
func main() {
engine = newEngine()
//execute()
//ormInsert()
ormGet()
ormGetCols()
//ormFindRows()
}
func newEngine() *xorm.Engine {
engine ,err := xorm.NewEngine(DriverName, MasterDataSourceName)
if err != nil {
log.Fatal(newEngine, err)
return nil
}
engine.ShowSQL(true)
return engine
}
func ormFindRows(){
list := make([]People, 0)
err := engine.Cols("id","age","name").Where("id>?",0).Limit(2).Asc("id").Find(&list)
if err == nil {
fmt.Printf("%v\n", list)
}else {
log.Fatal("ormFindRows error", err)
}
}
// insert by sql
func execute() {
sql := `INSERT INTO people VALUES(5, 23, 'cc')`
affected ,err := engine.Exec(sql)
if err != nil {
log.Fatal("execute error", err)
} else {
id, _ := affected.LastInsertId()
rows, _ := affected.RowsAffected()
fmt.Println("execute id : ", id , ", row = ", rows)
}
}
// insert by struct
func ormInsert () {
TempPeople := &People{
4,
11,
"danger",
}
id, err := engine.Insert(TempPeople)
if err != nil {
log.Fatal("ormInsert error", err)
} else {
fmt.Println("id",id)
fmt.Printf("%v\n", *TempPeople)
}
}
// search through query
func query() {
sql := "select * from people"
results, err := engine.QueryString(sql)
if err != nil {
log.Fatal("query error ", err)
}
totle := len(results)
if totle == 0 {
fmt.Println("no data")
}else {
for i, data := range results{
fmt.Printf("%d = %v\n" , i, data)
}
}
}
// search by models
func ormGet() {
TempPeople := &People{Id:2}
flag, err := engine.Get(TempPeople)
if flag {
fmt.Printf("%v\n", *TempPeople)
} else if err != nil {
log.Fatal("ormget error", err)
} else {
fmt.Println("ormget empty id = ", TempPeople.Id)
}
}
// search cols
// 仅查一部分col时,未查找的部分将返回0。
func ormGetCols() {
TempPeople := &People{Id:2}
flag, err := engine.Cols("name").Get(TempPeople)
if flag {
fmt.Printf("%v\n", *TempPeople)
} else if err != nil {
log.Fatal("ormget error", err)
} else {
fmt.Println("ormget empty id = ", TempPeople.Id)
}
}
func ormCount() {
count, err := engine.Count(&People{Name:"bb"})
if err == nil{
print("ok",count)
} else {
log.Fatal("error", err)
}
}
func ormUpdate() {
TempPeople := &People{Name:"zhangsan"}
flag, err := engine.ID(3).Update(TempPeople)
fmt.Println(flag, err)
}
// 排除一部分cols
func ormOmitUpdate() {
TempPeople := &People{Id:1}
flag, _ := engine.Get(TempPeople)
if flag {
flag, _ := engine.ID(TempPeople.Id).Omit("name").Update(&People{Id:12,Name:"1 to 12"})
fmt.Println("flag:", flag)
}
}
// 即使为空也更新cols
func ormMustColUpdate() {
TempPeople := &People{Id:1}
flag, _ := engine.Get(TempPeople)
if flag {
flag, _ := engine.ID(TempPeople.Id).MustCols("name").Update(&People{Id:12,Name:"1 to 12"})
fmt.Println("flag:", flag)
}
}
xorm的cmd工具
下载地址: "github.com/go-xorm/cmd/xorm"
个人觉得这种工具程序不能用mod进行下载管理,需要使用go get -u 的方式进行,因为mod下载后的内容不会编译成exe并移交到GOPATH\bin中。
总之,我们安装完成后,在bin目录下会发现一个可执行程序
在执行自动生成models之前,需要将模板导入到项目中,模板位置如下:
\GOPATH\src\github.com\go-xorm\cmd\xorm\templates\goxorm
然后执行reverse指令,就可以自动生成相关的model
xorm reverse mysql "root:*****@tcp(127.0.0.1:3306)/learn_go?charset=utf8" templates/goxorm/
文档链接:
最后附上个文档链接,供进一步学习使用。
https://www.kancloud.cn/kancloud/xorm-manual-zh-cn
网友评论