go get github.com/go-xorm/xorm #安装xorm
go get -u github.com/go-sql-driver/mysql #安装MySQL驱动
go mod tidy
go mod tidy
- 引用项目需要的依赖增加到go.mod文件。
- 去掉go.mod文件中项目不需要的依赖。
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"time"
)
func main() {
var (
userName string = "root"
password string = "root"
ipAddress string = "localhost"
port int = 3306
dbName string = "test"
)
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", userName, password, ipAddress, port, dbName)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败!")
}
type User struct {
Id int64
Name string
Age int
Passwd string `xorm:"varchar(200)"`
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
err = engine.Sync(new(User))
if err != nil {
fmt.Println("表结构同步失败")
}
}
插入单个数据
// 插入, 返回受影响的行数
user := User{
Id: 1000,
Name: "Xiaofang",
Age: 18,
Passwd: "123456",
}
n, _ := engine.Insert(&user)
if n > 1 {
fmt.Println("插入成功!")
}
插入多个参数
// 插入, 返回受影响的行数
user1 := User{
Id: 1000,
Name: "Xiaofang",
Age: 18,
Passwd: "123456",
}
user2 := user1
user2.Id = 1234
n, _ := engine.Insert(&user1, &user2)
if n > 1 {
fmt.Println("插入成功!")
}
或者
// 插入, 返回受影响的行数
user1 := User{
Id: 1000,
Name: "Xiaofang",
Age: 18,
Passwd: "123456",
}
user2 := user1
user2.Id = 1234
var users []User
users = append(users, user1)
users = append(users, user2)
n, _ := engine.Insert(users)
if n > 1 {
fmt.Println("插入成功!")
}
user := User{Name: "qinjiang"}
n, _ := engine.ID(1000).Update(&user)
n, _ = engine.ID(1000).Delete(user)
engine.Exec("update `user` set Age = 10 where id = ?", 10004)
fmt.Println(n)
// 查询
result1, err := engine.Query("select * from user")
result2, err := engine.QueryString("select * from user")
result3, err := engine.QueryInterface("select * from user")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Get
user := User{}
_, err = engine.Get(&user)
if err != nil {
return
}
fmt.Println(user)
// 指定条件
user1 := User{Name: "xiaofang"}
_, err = engine.Where("name=?", user1.Name).Desc("id").Get(&user1)
fmt.Println(user1)
var name string
_, err = engine.Table(&user).Where("id = 10001").Cols("name").Get(&name)
fmt.Println(name)
var users []User
err = engine.Where("passwd=123456").And("age=18").Limit(10, 0).Find(&users)
// Count 获取记录条数
var user4 User
user4 = User{Name: "kuangshen"}
_, err = engine.Count(&user4)
fmt.Println(user4)
// Iterate和Rows
engine.Iterate(&User{Passwd: "123456"}, func(idx int, bean interface{}) error {
user := bean.(*User)
fmt.Println(user)
return nil
})
rows, err := engine.Rows(&User{Passwd: "123456"})
defer rows.Close()
userBean := new(User)
for rows.Next() {
err := rows.Scan(userBean)
if err != nil {
return
}
fmt.Println(userBean)
}
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"time"
)
func main() {
var (
userName string = "root"
password string = "root"
ipAddress string = "localhost"
port int = 3306
dbName string = "test"
)
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", userName, password, ipAddress, port, dbName)
engine, err := xorm.NewEngine("mysql", dataSourceName)
if err != nil {
fmt.Println("数据库连接失败!")
}
type User struct {
Id int64
Name string
Age int
Passwd string `xorm:"varchar(200)"`
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
session := engine.NewSession()
defer session.Close()
err = session.Begin()
defer func() {
err := recover()
if err != nil {
session.Rollback()
} else {
session.Commit()
}
}()
user1 := User{Id: 10005, Name: "123", Age: 100, Passwd: "1234567890"}
if _, err := session.Insert(&user1); err != nil {
panic(err)
}
user2 := User{Name: "kuangshe222", Age: 3}
if _, err := session.Where("id=1000").Update(&user2); err != nil {
panic(err)
}
}