xorm用于在golang中链接数据库,并完成增删改差操作,不管是orm还是raw方式都十分的新颖简单。
sql语句
postgresql pgadmin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/*表结构*/
CREATE TABLE public . user
(
id serial primary key ,
name varchar (20)
);
ALTER TABLE public . user ADD COLUMN created timestamp default now();
ALTER TABLE public . user ADD COLUMN class_id integer default 1;
insert into public . user ( name ) values ( 'ft' );
insert into public . user ( name ) values ( 'ft2' );
insert into public . user ( name ,class_id) values ( 'ft8' ,2)
CREATE TABLE public .class
(
id serial primary key ,
name varchar (20)
);
insert into public .class( name ) values ( '高中一班' );
insert into public .class( name ) values ( '高中二班' );
/*常用到的操作*/
select * from public .class;
select * from public . user ;
select u.id,u. name ,c. name from public . user as u left join public .class as c on u.class_id=c.id;
|
main.go
每个功能都使用了if语句来方便开关,测试那块就打开哪块~~~
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
package main
import (
_ "github.com/lib/pq"
"github.com/xormplus/xorm"
"fmt"
"time"
"strconv"
)
//用户表结构
type User struct {
Id int `xorm: "not null pk autoincr INTEGER" `
Name string `xorm: "VARCHAR(20)" `
Created time . Time `xorm: "default 'now()' DATETIME" `
ClassId int `xorm: "default 1 INTEGER" `
}
//Class表结构
type Class struct {
Id int `xorm: "not null pk autoincr INTEGER" `
Name string `xorm: "VARCHAR(20)" `
}
//临时表结构
type UserClass struct{
User `xorm: "extends" `
Name string
}
//此方法仅用于orm查询时,查询表认定
func (UserClass) TableName() string {
return "public.user"
}
func main() {
//1.创建db引擎
db, err := xorm.NewPostgreSQL( "postgres://postgres:123@localhost:5432/test?sslmode=disable" )
if err != nil {
fmt.Println(err)
}
//2.显示sql语句
db.ShowSQL( true )
//3.设置连接数
db.SetMaxIdleConns(2000)
db.SetMaxOpenConns(1000)
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 500)//缓存的条数
db.SetDefaultCacher(cacher)
//4.执行插入语句的几种方式
//4.1 orm插入方式:不好控制,如果仅仅插入的对象的属性是 name = 'ftq' ,那么其他的零值会一同 insert ,orm方式对零值的处理有点不太好
if false {
user := new( User )
user . Name = "ftq"
_, err = db. Insert ( user )
if err != nil {
fmt.Println(err)
}
}
//4.2 命令插入方式
//4.2.1 db. Exec ():单事务单次提交
if false {
sql:= "insert into public.user(name) values(?)"
db. Exec (sql, "ft4" )
}
//4.2.2 db.SQL(). Execute ():单事务准备了Statement处理sql语句
if false {
sql:= "insert into public.user(name) values(?)"
db.SQL(sql, "ft5" ). Execute ()
}
//4.2.3使用sql配置文件管理语句,两种载入配置的方式LoadSqlMap()和RegisterSqlMap(),以及SqlMapClient()替代SQL()
if false {
err = db.LoadSqlMap( "./sql.xml" )
//err = db.RegisterSqlMap(xorm.Xml( "./" , "sql.xml" ))
if err != nil {
fmt.Println(err)
}
db.SqlMapClient( "insert_1" , "ft7" ). Execute ()
}
//5.执行查询的几种方式
//5.1 orm查询:在 user 初始化的时候,该orm查询直接通过扫描 user 类型确定表名;组合使用 Where (),Get()
if false {
user := new( User )
boolget ,err2 :=db. Where ( "id=?" ,5).Get( user )
fmt.Println(boolget,err2, user )
}
//5.2 orm查询:组合使用 Where (),Get(), And ()
if false {
user := new( User )
boolget ,err2 :=db. Where ( "id=?" ,5). And ( "name=?" , "ft7" ). And ( "id>?" ,3).Get( user )
fmt.Println(boolget,err2, user )
}
//5.3 orm查询: AllCols()查询所有列,Cols()查询部分列,Find()解析多行结果,Get()解析单行结果
if false {
users := new ([] User )
err = db.AllCols().Find(users)
//err = db.Cols( "id" , "name" ).Find(users)
if err !=nil {
fmt.Println(err)
}
fmt.Println(users)
}
//5.4 orm查询:连接查询 Join ()
if false {
users := new([]UserClass)
db. Join ( "INNER" , "class" , "user.class_id=class.id" ).Find(users)
//db.SQL( "select u.id,u.name,c.name from public.user as u left join public.class as c on u.class_id=c.id" ).Find(users)
fmt.Println(users)
}
//5.5 sql查询略
if false {
//和 insert 类似,Find查找多行结果,Get获取 单行结果
users := new([]UserClass)
db.SQL( "select u.id,u.name,c.name from public.user as u left join public.class as c on u.class_id=c.id" ).Find(users)
fmt.Println(users)
}
//5.6 链式查找
if false {
//值得一提的是,支持查找某行的某个字段,不过一般在sql语句中就可以完成过滤,如果sql语句过于复杂,可以链式查找过滤
id := db.SQL( "select * from public.user" ).Query().Results[0][ "id" ]
fmt.Println(id)
}
//6.执行更新
//6.1 ORM方式: 只有非0值的属性会被更新, user 的id和created都是默认零值,不被处理
if false {
user :=new( User )
user . Name = "ftx"
//[xorm] [info] 2018/02/08 12:04:01.330624 [SQL] UPDATE "user" SET "name" = $1 WHERE "id" =$2 []interface {}{ "ftx" , 4}
db.Id(4). Update ( user )
}
//6.2 SQL方式略,和 insert 类似
//7.事务
//7.1简单事务
if false {
session :=db.NewSession()
defer session. Close ()
session. Begin ()
//业务:新添加学生,并且创建新的班级,如果班级因为主键冲突创建失败,则整个事务回滚
_,err =session.SQL( "insert into public.user(name,class_id) values('ft13',2)" ). Execute ()
//表中已经有id=3的班级了
_,err =session.SQL( "insert into public.class(id,name) values(3,'高中3班')" ). Execute ()
if err!=nil {
session. Rollback ()
}
session. Commit ()
}
//7.2嵌套事务
if false {
session := db.NewSession()
defer session. Close ()
session. Begin ()
_,err=session. Exec ( "insert into public.user(name,class_id) values('ft23',2)" )
if err!=nil {
session. Rollback ()
}
_,err=session. Exec ( "insert into public.user(id,name,class_id) values(1,'ft24',2)" )
if err!=nil {
session. Rollback ()
}
tx,_:=session.BeginTrans()
_,err=tx.Session(). Exec ( "insert into public.user(name,class_id) values('ft25',2)" )
if err!=nil {
tx.RollbackTrans()
}
tx.CommitTrans()
session. Commit ()
}
//8.缓存:使用Raw方式修改以后,需要清理缓存
if true {
//建立500条数据
session := db.NewSession()
defer session. Close ()
if false {
session. Begin ()
for i := 30; i < 530; i++ {
value := "ft" + strconv.Itoa(i)
_, err = session. Exec ( "insert into public.user(name) values(?)" , value)
if err != nil {
session. Rollback ()
}
}
session. Commit ()
}
//查询前531条数据,并随意输出其中一条
users := make([] User ,10)
db.SQL( "select * from public.user where id<531 order by id" ).Find(&users)
fmt.Println( "读第一遍:" , "id:" ,users[50].Id, "name:" ,users[50]. Name )
db.SQL( "select * from public.user where id<531 order by id" ).Find(&users)
fmt.Println( "读第二遍:" , "id:" ,users[50].Id, "name:" ,users[50]. Name )
var step int =1
stepString := users[50]. Name + strconv.Itoa(step)
session. Exec ( "update public.user set name=? where id =45" ,stepString)
//清理缓存
db.ClearCache(new( User ))
time .Sleep(5* time . Second )
session.SQL( "select * from public.user where id<531 order by id" ).Find(&users)
fmt.Println( "读第三遍:" , "id:" ,users[50].Id, "name:" ,users[50]. Name )
//虽然很不好意思,但是就算开启了缓存数据也是脏了
}
//9.读写分离
if false {
//假设有多台服务器用来响应客户的读请求
var dbGroup *xorm.EngineGroup
conns :=[]string {
"postgres://postgres:123@localhost:5432/test?sslmode=disable" ,
"postgres://postgres:123@localhost:5432/test?sslmode=disable" ,
"postgres://postgres:123@localhost:5432/test?sslmode=disable" ,
"postgres://postgres:123@localhost:5432/test?sslmode=disable" ,
}
//负载均衡策略:(特性自行百度)
// 1.xorm.RandomPolicy()随机访问负载均衡,
// 2.xorm.WeightRandomPolicy([] int {2, 3,4})权重随机负载均衡
// 3.xorm.RoundRobinPolicy() 轮询访问负载均衡
// 4.xorm.WeightRoundRobinPolicy([] int {2, 3,4}) 权重轮训负载均衡
// 5.xorm.LeastConnPolicy()最小连接数负载均衡
dbGroup, err = xorm.NewEngineGroup( "postgres" , conns, xorm.RoundRobinPolicy())
//dbGroup使用方法和db一致
//简单查询
dbGroup.SQL( "inser into public.users(name) values('ft2000')" ). Execute ()
dbGroup. Exec ( "inser into public.users(name) values('ft2001')" )
//事务查询
session :=dbGroup.NewSession()
defer session. Close ()
session. Begin ()
_,err = session. Exec ( "inser into public.users(name) values('ft2001')" )
if err!=nil {
session. Rollback ()
}
session. Commit ()
}
}
//注意:
//1.postgresql好像不会默认按id增长排序,所以书写sql语句要提前写好 order by id ,楼主没怎么写,咳咳
//2. [5.4] postgresql建表会建在 public 策略的 table 里,所以查询语句表明写的是 public .xxxx,这也造成了连表orm查询会发生前缀报错,比如变成了 "SELECT * FROM " public "." user " INNER JOIN " class" ON user .class_id=class.id 这和内部的split有关,
//3.[8.]带的缓存好像很容易失效,在创建500个数据后,经过查查改查的操作,查询到的结果是一样的始终是一样的,本来改值之后应该最后一遍查会变化,然而并没有,缓存功能即使清理了缓存,还是会读到脏的
|
补充:golang xorm框架对应pg数据库字段类型参照表
1.左边是golang字段类型,右边是pg数据库字段类型
1
2
3
4
5
6
|
int integer
time . Time timestamp
int8 smallint
float64 numeric (8,3) (只要是 numeric 统统使用float64)
string text
string varchar
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/fwhezfwhez/article/details/79291492