-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe4e33e
commit 7a5c8ed
Showing
4 changed files
with
96 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module example | ||
|
||
require ( | ||
github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1 | ||
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect | ||
github.com/gin-gonic/gin v1.3.0 | ||
github.com/golang/protobuf v1.2.0 // indirect | ||
github.com/jinzhu/gorm v1.9.2 | ||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect | ||
github.com/mattn/go-isatty v0.0.4 // indirect | ||
github.com/mattn/go-sqlite3 v1.10.0 | ||
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect | ||
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect | ||
gopkg.in/yaml.v2 v2.2.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1 h1:NvwGCDDGv8Ax8SpwMhgujsrCr9gaSs5IWp90PP/PtSQ= | ||
github.com/biezhi/gorm-paginator v0.0.0-20190124081913-54ada098dbc1/go.mod h1:Ojq6HkxEz6814vSw/+tYrn79UpRl5qH6CsA1qxl/CgI= | ||
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY= | ||
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= | ||
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= | ||
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= | ||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= | ||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw= | ||
github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo= | ||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k= | ||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= | ||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= | ||
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= | ||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= | ||
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4= | ||
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= | ||
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/biezhi/gorm-paginator/pagination" | ||
"github.com/gin-gonic/gin" | ||
"github.com/jinzhu/gorm" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/biezhi/gorm-paginator/pagination" | ||
"github.com/gin-gonic/gin" | ||
"github.com/jinzhu/gorm" | ||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
// User 用户 | ||
type User struct { | ||
ID int | ||
UserName string `gorm:"not null;size:100;unique"` | ||
ID int | ||
UserName string `gorm:"not null;size:100;unique"` | ||
} | ||
|
||
func main() { | ||
db, err := gorm.Open("sqlite3", "example.db") | ||
if err == nil { | ||
db.AutoMigrate(&User{}) | ||
count := 0 | ||
db.Model(User{}).Count(&count) | ||
if count == 0 { | ||
db.Create(User{ID: 1, UserName: "biezhi"}) | ||
db.Create(User{ID: 2, UserName: "rose"}) | ||
db.Create(User{ID: 3, UserName: "jack"}) | ||
db.Create(User{ID: 4, UserName: "lili"}) | ||
db.Create(User{ID: 5, UserName: "bob"}) | ||
db.Create(User{ID: 6, UserName: "tom"}) | ||
db.Create(User{ID: 7, UserName: "anny"}) | ||
db.Create(User{ID: 8, UserName: "wat"}) | ||
fmt.Println("Insert OK!") | ||
} | ||
} else { | ||
fmt.Println(err) | ||
return | ||
} | ||
db, err := gorm.Open("sqlite3", "example.db") | ||
if err == nil { | ||
db.AutoMigrate(&User{}) | ||
count := 0 | ||
db.Model(User{}).Count(&count) | ||
if count == 0 { | ||
db.Create(User{ID: 1, UserName: "biezhi"}) | ||
db.Create(User{ID: 2, UserName: "rose"}) | ||
db.Create(User{ID: 3, UserName: "jack"}) | ||
db.Create(User{ID: 4, UserName: "lili"}) | ||
db.Create(User{ID: 5, UserName: "bob"}) | ||
db.Create(User{ID: 6, UserName: "tom"}) | ||
db.Create(User{ID: 7, UserName: "anny"}) | ||
db.Create(User{ID: 8, UserName: "wat"}) | ||
fmt.Println("Insert OK!") | ||
} | ||
} else { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
var users []User | ||
var users []User | ||
|
||
pagination.Pagging(&pagination.Param{ | ||
DB: db.Where("id > ?", 0), | ||
Result: &users, | ||
Page: 1, | ||
Limit: 3, | ||
OrderBy: []string{"id desc"}, | ||
ShowSQL: true, | ||
}) | ||
pagination.Paging(&pagination.Param{ | ||
DB: db.Where("id > ?", 0), | ||
Page: 1, | ||
Limit: 3, | ||
OrderBy: []string{"id desc"}, | ||
ShowSQL: true, | ||
}, &users) | ||
|
||
fmt.Println("users:", users) | ||
fmt.Println("users:", users) | ||
|
||
r := gin.Default() | ||
r.GET("/", func(c *gin.Context) { | ||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) | ||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "3")) | ||
var users []User | ||
r := gin.Default() | ||
r.GET("/", func(c *gin.Context) { | ||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) | ||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "3")) | ||
var users []User | ||
|
||
paginator := pagination.Pagging(&pagination.Param{ | ||
DB: db, | ||
Result: &users, | ||
Page: page, | ||
Limit: limit, | ||
OrderBy: []string{"id desc"}, | ||
ShowSQL: true, | ||
}) | ||
c.JSON(200, paginator) | ||
}) | ||
paginator := pagination.Paging(&pagination.Param{ | ||
DB: db, | ||
Page: page, | ||
Limit: limit, | ||
OrderBy: []string{"id desc"}, | ||
ShowSQL: true, | ||
}, &users) | ||
c.JSON(200, paginator) | ||
}) | ||
|
||
r.Run() | ||
r.Run() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters