golang透過mysql語句實現分頁查詢
1.前端介面呼叫
2.register訪問入口
//查詢一個使用者下所有的subnet ws.Route(ws.GET("/subnets"). To(sc.ListSubnet). Doc("List subnets authorized to the login user."). Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")). Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)). Returns(http.StatusOK, api.StatusOK, api.ListResult{}))
3.解析引數
//查詢某個使用者下所有的subnet資訊 func (v *SubController) ListSubnet(request *restful.Request, response *restful.Response) { username := request.Attribute(filters.UserName).(string) subnetService := &service.SubnetService{} query := query.ParseQueryParameter(request) result, err := subnetService.ListSubnet(query, v.Db, username) if err != nil { api.HandleError(response, request, err) return } response.WriteEntity(result) }
4.service實現
//查詢一個使用者所有的subnet資訊 func (ss *SubnetService) ListSubnet(query *query.Query, db *sql.DB, userName string) (*api.ListResult, error) { sm := mapper.NewSubnetMapper(db) sb, err := sm.SearchAllByUserName(query, userName) return sb, err }
5.mapper實現
//查詢使用者下的所以資訊 func (s *SubnetMapper) SearchAllByUserName(query *query.Query, userName string) (*api.ListResult, error) { totalRow, err := s.Db.Query("SELECT COUNT(*) from t_subnet ,t_vpc where t_vpc.id = t_subnet.vpcid and username = ?", userName) if err != nil { klog.Error("query orders count error", err) return nil, err } total := 0 for totalRow.Next() { err := totalRow.Scan( &total, ) if err != nil { klog.Error("query orders count error", err) continue } } totalRow.Close() rows, err := s.Db.Query(SearchAllByUserNameSql, userName, query.Pagination.Limit, query.Pagination.Offset) defer func() { if rows != nil { rows.Close() } }() if err != nil { klog.Error("query subnet error", err) return nil, err } items := make([]interface{}, 0) for rows.Next() { ss := new(vpc.SubnetItem) err = rows.Scan(&ss.VpcName, &ss.VpcNetwork, &ss.SubnetName, &ss.SubnetNetwork, &ss.CreateTime) items = append(items, *ss) } return &api.ListResult{ TotalItems: total, Items: items, }, nil }
可以看到第一步查出了所有count數目:
查詢語句加上引數。控制查到的頁數。
返回的是總數量跟當前查詢頁。
特殊情況用不到資料庫分頁的,就需要用到記憶體分頁了.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69901823/viewspace-2887441/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- golang通過mysql語句實現分頁查詢GolangMySql
- oracle分頁查詢語句Oracle
- [Mysql 查詢語句]——分組查詢group byMySql
- Oracle分頁查詢語句(七)Oracle
- Oracle分頁查詢語句(六)Oracle
- Oracle分頁查詢語句(五)Oracle
- Oracle分頁查詢語句(四)Oracle
- Oracle分頁查詢語句(三)Oracle
- Oracle分頁查詢語句(二)Oracle
- Oracle分頁查詢語句(一)Oracle
- MySQL、Oracle和SQL Server的分頁查詢語句MySqlOracleServer
- MYSQL SQLServer分頁查詢的實現MySqlServer
- Sql Server系列:查詢分頁語句SQLServer
- 有關Oracle分頁查詢語句Oracle
- mysql查詢語句MySql
- indexdb實現分頁查詢Index
- oracle語法相容--如何透過with recursive語法來實現oracle的分層查詢Oracle
- [Mysql 查詢語句]——查詢欄位MySql
- mysql查詢語句集MySql
- MySQL查詢阻塞語句MySql
- Mysql之查詢語句MySql
- Golang實現二分查詢法Golang
- MySQL 查詢語句執行過程淺析MySql
- mysql查詢語句5:連線查詢MySql
- [Mysql 查詢語句]——查詢指定記錄MySql
- MySQL的分頁查詢MySql
- MySQL 多表查詢分頁MySql
- 透過DMV查詢CPU時間最長的語句和查詢計劃
- mysql高階查詢語句MySql
- MySQL基礎查詢語句MySql
- c# winform 實現分頁查詢C#ORM
- SSM框架實現分頁查詢例子SSM框架
- MySQL分頁查詢優化MySql優化
- MySQL內連線查詢語句MySql
- MySQL的簡單查詢語句MySql
- mysql dba常用的查詢語句MySql
- [Mysql 查詢語句]——集合函式MySql函式
- mysql查詢語句優化工具MySql優化