Giter Site home page Giter Site logo

gorm-paginator's Introduction

简介

A gorm paginator, based on generic of go 1.18
基于Go 1.18泛型对Gorm分页的简单封装

安装

go get -u github.com/yafeng-Soong/gorm-paginator

使用

内置的分页结构体

type Page[T any] struct {
	CurrentPage int64 `json:"currentPage"`
	PageSize    int64 `json:"pageSize"`
	Total       int64 `json:"total"`
	Pages       int64 `json:"pages"`
	Data        []T   `json:"data"`
}

分页查询函数SelectPages封装为了Page的结构体方法。使用时只需要提前设置好当前页数、分页大小,再传入设置好查询条件的gorm.DB结构体指针即可,如:

func test() {
    // 先构建查询条件
	query := db.Where("Continent = ? and IndepYear > ?", "Asia", 1900)
    // 构建Page结构体,设置当前页数和分页大小
	page := paginator.Page[Country]{CurrentPage: 1, PageSize: 15}
	page.SelectPages(query) // 执行分页查询
}

完整示例

package paginator_test

import (
	"log"
	"testing"

	paginator "github.com/yafeng-Soong/gorm-paginator"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type Country struct {
	Code      string `json:"code"`
	Name      string `json:"name"`
	Continent string `json:"continent"`
	Region    string `json:"region"`
	IndepYear int    `json:"indepYear"`
}

func (c *Country) TableName() string {
	return "country"
}

func Test_page(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/world?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		log.Fatal("connect error")
	}
	query := db.Where("Continent = ? and IndepYear > ?", "Asia", 1900)
	p := paginator.Page[Country]{CurrentPage: 1, PageSize: 15}
	p.SelectPages(query)
	log.Println(p.CurrentPage)
	log.Println(p.PageSize)
	log.Println(p.Total)
	log.Println(p.Pages)
	log.Println(p.Data)
}

gorm-paginator's People

Contributors

yafeng-soong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gorm-paginator's Issues

使用原生 sql 查询时候无效

RT,自己写了 sql 在字符串里,然后使用 query.Raw(queryStr, values...) 设置,同时用了分页查询,但是这查询无效了。开启Debug模式发现 sql 就没有运行,目前正在研究。

image

I'd like to contribute to your code!

Hi!

First I'd like to say this is the best paginator for gorm out there!. Really, I was looking for something as easy to user and adapt for my projects! Thank to take the time to write this!

I was wondering if you have a contribution guideline. I'd like to add something to this repo and i want to do it right!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.