-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.go
52 lines (40 loc) · 1.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/xujiajun/godbal"
"github.com/xujiajun/godbal/driver/mysql"
)
func main() {
database, err := godbal.NewMysql("root:123@tcp(127.0.0.1:3306)/test?charset=utf8").Open()
if err != nil {
panic(err)
}
err = database.Ping()
if err != nil {
panic(err)
}
queryBuilder := mysql.NewQueryBuilder(database)
sql := queryBuilder.Select("uid,username,price,flag").From("userinfo", "").SetFirstResult(0).
SetMaxResults(5).OrderBy("uid", "DESC").OrderBy("username", "DESC").GetSQL()
fmt.Println(sql)
rows, _ := queryBuilder.QueryAndGetMap()
jsonString, _ := json.Marshal(&rows)
fmt.Println(string(jsonString))
queryBuilder2 := mysql.NewQueryBuilder(database)
sql = queryBuilder2.Select("uid,username,created,textVal,price,name").From("userinfo", "").Where("username = ? AND departname = ?").
SetParam("johnny2").SetParam("tec").SetFirstResult(0).
SetMaxResults(3).OrderBy("uid", "DESC").GetSQL()
fmt.Println(sql)
rows2, _ := queryBuilder2.QueryAndGetMap()
jsonString2, _ := json.Marshal(&rows2)
fmt.Println(string(jsonString2))
queryBuilder3 := mysql.NewQueryBuilder(database)
rows3, _ := queryBuilder3.Select("uid,username,created,textVal,price,name").From("userinfo", "").Where("username = ?").
SetParam("johnny2").SetFirstResult(0).
SetMaxResults(1).OrderBy("uid", "DESC").QueryAndGetMap()
fmt.Println()
jsonString3, _ := json.Marshal(&rows3)
fmt.Println(string(jsonString3))
}