xorm使用

2021/9/14 23:09:10

本文主要是介绍xorm使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

xorm使用

在d:\goxorm新建俩个文件

config

lang=go
genJson=1
prefix=cos_

 struct.go.tpl 

package {{.Models}}

{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
    {{range .Imports}}"{{.}}"{{end}}
)
{{end}}

{{range .Tables}}
type {{Mapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{Mapper $col.Name}}    {{Type $col}} {{Tag $table $col}}
{{end}}
}

{{end}}  

在go终端执行以下命令

D:\>xorm reverse postgres  postgres://postgres:asdf@localhost:5432/postgres?sslmode=disable goxorm

会在d:\modals自动生成数据库所有表的XORM需要的结构体(也可以手动,如果数据表多,最好还是自动生成)

type Tunit struct {
	Unitid   string `json:"unitid" xorm:"not null pk VARCHAR(9)"`
	Unitname string `json:"unitname" xorm:"VARCHAR(9)"`
}

 测试程序 

package main

import (
    "fmt"

    _ "github.com/lib/pq"
    "xorm.io/xorm"
)

type Tunit struct {
    Unitid   string `json:"unitid" xorm:"not null pk VARCHAR(9)"`
    Unitname string `json:"unitname" xorm:"VARCHAR(9)"`
}

func main() {
    db, err := xorm.NewEngine("postgres", "postgres://postgres:asdf@localhost:5432/postgres?sslmode=disable")
    if err != nil {
        fmt.Println(err)
    }
    //2.显示sql语句
    //db.ShowSQL(true)
    //3.设置连接数
    db.SetMaxIdleConns(2000)
    db.SetMaxOpenConns(1000)

    // 查询数组,find的第二个可选参数是查询条件bean
    var s []Tunit
    db.Cols().Find(&s)
    fmt.Println(s)
}

 



这篇关于xorm使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程