package main
import (
"fmt" "time" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/core" "github.com/go-xorm/xorm")type Member struct {
Memberid int64 `xorm:"int(10) pk not null autoincr 'member_id'"` Member_name string `xorm:"char(50) not null"` Member_pwd string `xorm:"char(32) not null"` Member_count int64 `xorm:"smallint(6) not null default 0 "` Member_ok string `xorm:"varchar(50) not null"` Member_del int64 `xorm:"bigint(1) not null default 0 "` Member_email string `xorm:"varchar(40) not null"` Member_logintime time.Time `xorm:"updated"` Member_createtime int64 `xorm:"int(11) not null default 0"` Member_updatetime int64 `xorm:"int(11) not null default 0"` Member_ip string `xorm:"varchar(40) not null"` Member_version int64 `xorm:"version"`}var (
engine *xorm.Engine)func main() {
//数据库连接参数 params := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", "root", "root", "localhost:3306", "go") var err error //连接数据库 engine, err = xorm.NewEngine("mysql", params) if err != nil { panic(err) } //添加统一前缀 tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "go_") engine.SetTableMapper(tbMapper) defer engine.Close()member := new(Member)
//Count查询总条数 total, terr := engine.Count(member) if terr != nil { panic(terr) } fmt.Println("go_member总信息条数为: ", total, "条")//Rows逐条执行查询到的记录
getrow := member rows, rerr := engine.Where("member_id > ?", 1).Desc("member_id").Limit(10,0).Rows(getrow) if rerr != nil { panic(rerr) } defer rows.Close() for rows.Next() { rerr = rows.Scan(getrow) if rerr != nil { panic(rerr) } fmt.Println(getrow) }}