美文网首页
go xorm学习

go xorm学习

作者: 漯河龙 | 来源:发表于2017-08-20 00:44 被阅读0次

    1.方法Update接受的第一个参数必须是指针地址,指向需要更新的内容

    2.更新的条件

    _,err=x.Cols("data","code_type").Update(cd,&Code{Phone:phone})

    log.Fatalf("err %v",err)  打印错误并中断程序

    fmt.Scanf("%d\n",&num)

    %s %f

    初始化对象

    ac:=new(Account)

    ac.Name = "张三"

    ac.Balance = 65

    ad :=&Account{Name:"李四",Balance:68}

    _,err:=x.Insert(ac,ad)

    Exit:

    for {

    break Exit //退出循环

    }

    fmt.Printf("%s%v\n",a.Name,a.Balance)

    fmt.Printf("%#v",a) //打印全部信息

    李四68

    &model.Account{Id:2, Name:"李四", Balance:68, Verson:1}

    //初始化异常

    return nil,errors.New("acctoun not found")

    整型---转成字符串

    fmt.Sprintf("%d",i*11111)

    %v\n


    packagemodel

    import(

    _"github.com/go-xorm/xorm"

    _"github.com/go-sql-driver/mysql"

    "github.com/go-xorm/xorm"

    "log"

    "errors"

    "fmt"

    "os"

    )

    //迭代

    varprint =func(idxint,beaninterface{})error{

    fmt.Printf("%d==%#v\n",idx,bean.(*Account))

    returnnil

    }

    typeAccountstruct{

    Idint64

    Namestring

    Balancefloat64

    Sexint64

    Versonint`xorm:"version"`

    CardIdstring`xorm:"unique"`

    }

    varx *xorm.Engine

    funcinit() {

    varerrerror

    x,err = xorm.NewEngine("mysql","root:@/mygoweb?charset=utf8")

    iferr!=nil {

    log.Fatalf("err %v",err)

    }

    iferr=x.Sync(new(Account));err!=nil {

    log.Fatalf("err %v",err)

    }

    }

    //增加

    funcNewAccount()error{

    ac:=new(Account)

    ac.Name ="张三"

    ac.Balance =65

    ad :=&Account{Name:"李四",Balance:68}

    _,err:=x.Insert(ac,ad)

    returnerr

    }

    funcNewAccount2(sizeint64)error{

    count:=GetAccountsCouont()

    fori:=count;i

    if_,err:=x.Insert(&Account{Name:fmt.Sprintf("王五%d",i),Balance:float64(i)*100,CardId:fmt.Sprintf("%d",i*11111)});err!=nil {

    returnerr

    }

    }

    returnnil

    }

    //查询

    funcGetAccount(idint64)  (*Account,error){

    a:=&Account{}

    has,err:=x.Id(id).Get(a)

    iferr!=nil {

    returnnil,err

    }else if!has {

    returnnil,errors.New("acctoun not found")

    }

    returna,nil

    }

    //查询更多 切记一定要传入 &as

    funcGetAccounts()  (as []*Account,errerror){

    err=x.Asc("id").Find(&as)

    returnas,err

    }

    //获取数量

    funcGetAccountsCouont()  (int64){

    count,_:=x.Count(new(Account))

    //count,_:=x.Where("id>10").Count(new(Account))

    returncount

    }

    //更新

    funcSaveMoney(idint64,monefloat64)  (*Account,error)  {

    a,err:=GetAccount(id)

    iferr!=nil {

    returnnil,err

    }

    a.Balance+=mone

    //_,err=x.Update(a)

    _,err=x.Cols("balance").Update(a)  

    iferr!=nil {

    returnnil,err

    }

    returna,nil

    }

    //更新

    funcGetMoney(idint64,monefloat64)  (*Account,error)  {

    a,err:=GetAccount(id)

    iferr!=nil {

    returnnil,err

    }

    ifa.Balance<=mone {

    returnnil,errors.New("money not enthrog")

    }

    a.Balance -= mone

    _,err=x.Cols("balance").Update(a)

    iferr!=nil {

    returnnil,err

    }

    returna,nil

    }

    //安全性

    funcGetMoney2(idint64,monefloat64)  (*Account,error)  {

    a,err:=GetAccount(id)

    iferr!=nil {

    returnnil,err

    }

    ifa.Balance<=mone {

    returnnil,errors.New("money not enthrog")

    }

    a.Balance -= mone

    //创建事务并且开启事务

    session:=x.NewSession()

    defersession.Close()

    iferr =session.Begin();err!=nil {

    returnnil,err

    }

    //通过事务执行语句

    _,err=session.Cols("balance").Update(a)

    iferr!=nil {

    //报错就回滚

    session.Rollback()

    returnnil,err

    }

    //没有报错就提交事务

    session.Commit()

    returna,nil

    }

    funcUpdateSex(ac *Account)  (*Account,error)  {

    _,err:=x.Cols("sex").Update(ac)

    iferr!=nil {

    returnnil,err

    }

    returnac,nil

    }

    funcDeleteAccount(idint64)  (errerror)  {

    _,err=x.Delete(&Account{Id:id})

    returnerr

    }

    //迭代

    funcPrintAcount()  {

    x.Iterate(new(Account),print)

    }

    //迭代打印名字和余额的值,其他值为默认值

    funcGetAcountName()  {

    x.Cols("name","balance").Iterate(new(Account),print)

    }

    //打印不包括性别的值,其他值正常打印

    funcGetAcountNoSex()  {

    x.Omit("sex").Iterate(new(Account),print)

    }

    //分页获取数据

    funcGetAcountPage(page,sizeint)  {

    //取3条从第page*size开始

    page-=1

    x.Limit(size,page*size).Iterate(new(Account),print)

    }

    //日志功能

    funcLogSql() {

    //f,err:=os.OpenFile("sql.log",os.O_CREATE|os.ModeAppend,"")

    f,err := os.Create("sql.log")

    iferr != nil {

    log.Fatalf("%v",err)

    }

    x.SetLogger(xorm.NewSimpleLogger(f))

    x.ShowSQL(true)

    }

    //设置缓存

    funcSetCache(){

    cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(),1000)

    x.SetDefaultCacher(cacher)

    }


    packagemain

    import(

    "xormapp/model"

    "log"

    "fmt"

    )

    funcmain() {

    er:= model.NewAccount2(10)

    ifer!=nil {

    log.Fatalf("err=%v",er)

    }

    a,err:=model.GetAccount(2)

    iferr!=nil {

    log.Fatalf("err=%v",err)

    }

    fmt.Printf("%s%v\n",a.Name,a.Balance)

    fmt.Printf("%#v\n",a)

    //a,err=model.GetMoney(1,50.25)

    //if err!=nil {

    // log.Fatalf("err=%v",err)

    //}

    //fmt.Printf("%#v",a)

    //as,err:=model.GetAccounts()

    //if err!=nil {

    // log.Fatalf("err=%v",err)

    //}

    //for _,a:=range as {

    // a.Sex =1

    // model.UpdateSex(a)

    // fmt.Printf("%#v",a)

    //}

    //err= model.DeleteAccount(1)

    //if err!=nil {

    // log.Fatalf("err=%v",err)

    //}

    //model.PrintAcount()

    //分页

    //model.GetAcountPage(1,3)

    //model.GetAcountName()

    model.GetAcountNoSex()

    }

    相关文章

      网友评论

          本文标题:go xorm学习

          本文链接:https://www.haomeiwen.com/subject/hubxdxtx.html