Skip to content

Commit 2e88a9c

Browse files
committed
增加实现和readme说明
1 parent 1691ec0 commit 2e88a9c

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
*.zip
18+
/.idea/

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# pagehelper
2+
3+
## 介绍
4+
5+
pagehelper是与[gobatis](https://github.com/xfali/gobatis)配套的分页工具
6+
7+
## 待完成项
8+
9+
select查询返回的result还未能携带page、pageSize、total信息,需要自己组装。
10+
11+
(此功能需gobatis支持,暂未实现)
12+
13+
## 使用
14+
15+
### 1、使用pagehelper的Factory生成SessionManager
16+
17+
```cassandraql
18+
fac := factory.DefaultFactory{
19+
Host: "localhost",
20+
Port: 3306,
21+
DBName: "test",
22+
Username: "root",
23+
Password: "123",
24+
Charset: "utf8",
25+
26+
MaxConn: 1000,
27+
MaxIdleConn: 500,
28+
29+
Log: logging.DefaultLogf,
30+
}
31+
sessMgr := gobatis.NewSessionManager(pagehelper.New(&fac))
32+
```
33+
34+
### 2、配置分页参数
35+
```cassandraql
36+
session := sessMgr.NewSession()
37+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
38+
ctx = pagehelper.StartPage(1, 2, ctx)
39+
40+
var ret []TestTable
41+
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
42+
```

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github/xfali/pagehelper
2+
3+
go 1.12
4+
5+
require github.com/xfali/gobatis v0.0.4

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
2+
github.com/xfali/gobatis v0.0.4 h1:rBR8m18xMQAL58jDIr+i9zj8RYC+Mfa9rOw4ZB3shf0=
3+
github.com/xfali/gobatis v0.0.4/go.mod h1:hXP3PcXMSYpYFHQejG04qQjSQJtrt8jWpslftkUMBrg=

pagehelper.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (C) 2019, Xiongfa Li.
3+
* All right reserved.
4+
* @author xiongfa.li
5+
* @version V1.0
6+
* Description:
7+
*/
8+
9+
package pagehelper
10+
11+
import (
12+
"context"
13+
"fmt"
14+
"github.com/xfali/gobatis/executor"
15+
"github.com/xfali/gobatis/factory"
16+
"github.com/xfali/gobatis/reflection"
17+
"github.com/xfali/gobatis/transaction"
18+
"strings"
19+
)
20+
21+
const (
22+
pageHelperValue = "_page_helper_value"
23+
)
24+
25+
type PageParam struct {
26+
Page int
27+
PageSize int
28+
}
29+
30+
func New(f *factory.DefaultFactory) *PageHelperFactory {
31+
return &PageHelperFactory{*f}
32+
}
33+
34+
type PageHelperFactory struct {
35+
factory.DefaultFactory
36+
}
37+
38+
type PageHelperExecutor struct {
39+
executor.SimpleExecutor
40+
}
41+
42+
func StartPage(page, pageSize int, ctx context.Context) context.Context {
43+
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize})
44+
}
45+
46+
func (exec *PageHelperExecutor) Query(ctx context.Context, result reflection.Object, sql string, params ...interface{}) error {
47+
p := ctx.Value(pageHelperValue)
48+
if p != nil {
49+
if param, ok := p.(*PageParam); ok {
50+
sql = modifySql(sql, param)
51+
}
52+
}
53+
54+
return exec.SimpleExecutor.Query(ctx, result, sql, params...)
55+
}
56+
57+
func (f *PageHelperFactory) CreateExecutor(transaction transaction.Transaction) executor.Executor {
58+
return &PageHelperExecutor{*executor.NewSimpleExecutor(transaction)}
59+
}
60+
61+
func modifySql(sql string, p *PageParam) string {
62+
b := strings.Builder{}
63+
b.WriteString(sql)
64+
b.WriteString(fmt.Sprintf(" OFFSET %d LIMIT %d ", p.Page*p.PageSize, p.PageSize))
65+
return b.String()
66+
}

pagehelper_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Copyright (C) 2019, Xiongfa Li.
3+
* All right reserved.
4+
* @author xiongfa.li
5+
* @version V1.0
6+
* Description:
7+
*/
8+
9+
package pagehelper
10+
11+
import (
12+
"context"
13+
"github.com/xfali/gobatis"
14+
"github.com/xfali/gobatis/factory"
15+
"github.com/xfali/gobatis/logging"
16+
"testing"
17+
"time"
18+
)
19+
20+
type TestTable struct {
21+
TestTable gobatis.ModelName "test_table"
22+
Id int64 `xfield:"id"`
23+
Username string `xfield:"username"`
24+
Password string `xfield:"password"`
25+
}
26+
27+
func TestPageHelper(t *testing.T) {
28+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
29+
ctx = StartPage(1, 2, ctx)
30+
31+
p := ctx.Value(pageHelperValue)
32+
printV(t, p)
33+
34+
select {
35+
case <-ctx.Done():
36+
break
37+
}
38+
printV(t, p)
39+
}
40+
41+
func TestPageHelper2(t *testing.T) {
42+
fac := factory.DefaultFactory{
43+
Host: "localhost",
44+
Port: 3306,
45+
DBName: "test",
46+
Username: "root",
47+
Password: "123",
48+
Charset: "utf8",
49+
50+
MaxConn: 1000,
51+
MaxIdleConn: 500,
52+
53+
Log: logging.DefaultLogf,
54+
}
55+
sessMgr := gobatis.NewSessionManager(New(&fac))
56+
session := sessMgr.NewSession()
57+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
58+
ctx = StartPage(1, 2, ctx)
59+
60+
session.SetContext(ctx)
61+
62+
var ret []TestTable
63+
session.Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
64+
}
65+
66+
func TestModify(t *testing.T) {
67+
sql := modifySql("select * from x", &PageParam{1, 2})
68+
t.Log(sql)
69+
}
70+
71+
func printV(t *testing.T, p interface{}) {
72+
if p, ok := p.(*PageParam); ok {
73+
t.Logf("param: %d %d", p.Page, p.PageSize)
74+
} else {
75+
t.Fail()
76+
}
77+
}
78+
79+
func TestContext(t *testing.T) {
80+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
81+
ctx = context.WithValue(ctx, "1", "a")
82+
83+
t.Log(ctx.Value("1"))
84+
select {
85+
case <-ctx.Done():
86+
break
87+
}
88+
t.Log(ctx.Value("1"))
89+
}
90+
91+
type A struct{ I int }
92+
type B struct{ A }
93+
94+
func TestStruct(t *testing.T) {
95+
a := &A{10}
96+
b := B{*a}
97+
t.Logf("b:%d\n", b.I)
98+
if b.I != 10 {
99+
t.Fail()
100+
}
101+
}

0 commit comments

Comments
 (0)