golang數據庫分頁(通用化)[go-xorm分頁]

請不要爲寫業務翻頁煩惱~!

博主寫了個比較通用的,不多說,直接上代碼,如果有機會能集成到xorm庫裏,也可以~!

PS : po直接映射字段,請注意需要json配置,不需要再直接寫Fields了~ QueryString因爲不帶數據類型,拿出來轉換比較麻煩

具體請看 : https://github.com/ycc297876771/xorm-pagenation

package main

import (
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"fmt"
	"bill/src/pagenation"
)

//po base database => xorm
type DeviceOauthLogPo struct {
	DeviceType         int    `json:"device_type"`
	DeviceTypeName     string `json:"device_type_name"`
	DeviceTypeTitle    string `json:"device_type_title"`
	DeviceScreenWidth  int    `json:"device_screen_width"`
	DeviceScreenHeight int    `json:"device_screen_height"`
}

func main() {
	engine, err := xorm.NewEngine( "mysql",
		"root" + ":" + "root" + "@tcp("+"127.0.0.1"+":"+ "3306" +")/"+ "bill"+"?charset=utf8",
	)
	if err != nil{
		fmt.Print(err.Error())
	}
	var ListHelper = pagenation.DaoBase{}
	ListHelper.SetDatasource(engine)
	var po = []*DeviceOauthLogPo{}
	ListHelper.GetLists(&po,"`test`","`id`","","`id` DESC")
	if len(po) > 0{
		for _,row := range po{
			fmt.Print(row.DeviceTypeName)
			fmt.Print("\r\n ========= \r\n")
			fmt.Print(row.DeviceTypeTitle)
			fmt.Print("\r\n ========= \r\n")
		}
	}

	var po1 = []*DeviceOauthLogPo{}
	var pageListInfo = ListHelper.GetPageLists(&po1,"`test`","`id`","","`id` DESC",0,2)
	if len(po) > 0{
		fmt.Print(pageListInfo)    //result : map[string]interface{} 請閱讀源碼
	}

	fmt.Print("\r\n ========== condition Build \r\n")

	//condition Build  後面可以自己擴展
	var condi = make(map[string]map[string]interface{})
	var inCodi = make(map[string]interface{})
	var like = make(map[string]interface{})
	like["name"] = "Bill"
	inCodi["type"] = 1
	inCodi["title"] = "Bill"
	condi["AND"] = inCodi
	condi["LIKE"] = like
	fmt.Print(ListHelper.ConditionBuild(condi))
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章