{"id":239313,"date":"2022-04-16T10:01:16","date_gmt":"2022-04-16T02:01:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239313"},"modified":"2022-03-30T10:02:40","modified_gmt":"2022-03-30T02:02:40","slug":"golang-paging-query","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/golang-paging-query.html","title":{"rendered":"golang\u901a\u8fc7mysql\u8bed\u53e5\u5b9e\u73b0\u5206\u9875\u67e5\u8be2"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\ngolang\u901a\u8fc7mysql\u8bed\u53e5\u5b9e\u73b0\u5206\u9875\u67e5\u8be2\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u524d\u7aef\u63a5\u53e3\u8c03\u7528<\/strong><\/div>\n

\"\"<\/p>\n

2.register\u8bbf\u95ee\u5165\u53e3<\/strong><\/div>\n
\r\n\/\/\u67e5\u8be2\u4e00\u4e2a\u7528\u6237\u4e0b\u6240\u6709\u7684subnet\r\nws.Route(ws.GET(\"\/subnets\").\r\nTo(sc.ListSubnet).\r\nDoc(\"List subnets authorized to the login user.\").\r\nParam(ws.QueryParameter(query.ParameterPage, \"page\").Required(false).DataFormat(\"page=%d\").DefaultValue(\"page=1\")).\r\nParam(ws.QueryParameter(query.ParameterLimit, \"limit\").Required(false)).\r\nReturns(http.StatusOK, api.StatusOK, api.ListResult{}))\r\n<\/pre>\n
3.\u89e3\u6790\u53c2\u6570<\/strong><\/div>\n
\r\n\/\/\u67e5\u8be2\u67d0\u4e2a\u7528\u6237\u4e0b\u6240\u6709\u7684subnet\u4fe1\u606f\r\nfunc (v *SubController) ListSubnet(request *restful.Request, response *restful.Response) {\r\nusername := request.Attribute(filters.UserName).(string)\r\n \r\nsubnetService := &service.SubnetService{}\r\nquery := query.ParseQueryParameter(request)\r\nresult, err := subnetService.ListSubnet(query, v.Db, username)\r\nif err != nil {\r\napi.HandleError(response, request, err)\r\nreturn\r\n}\r\nresponse.WriteEntity(result)\r\n}\r\n<\/pre>\n

\"\"<\/p>\n

4.service\u5b9e\u73b0<\/strong><\/div>\n
\r\n\r\n\/\/\u67e5\u8be2\u4e00\u4e2a\u7528\u6237\u6240\u6709\u7684subnet\u4fe1\u606f\r\nfunc (ss *SubnetService) ListSubnet(query *query.Query, db *sql.DB, userName string) (*api.ListResult, error) {\r\nsm := mapper.NewSubnetMapper(db)\r\nsb, err := sm.SearchAllByUserName(query, userName)\r\nreturn sb, err\r\n}\r\n<\/pre>\n
5.mapper\u5b9e\u73b0<\/strong><\/div>\n
\r\n\/\/\u67e5\u8be2\u7528\u6237\u4e0b\u7684\u6240\u4ee5\u4fe1\u606f\r\nfunc (s *SubnetMapper) SearchAllByUserName(query *query.Query, userName string) (*api.ListResult, error) {\r\n \r\ntotalRow, err := s.Db.Query(\"SELECT COUNT(*) from t_subnet ,t_vpc where t_vpc.id = t_subnet.vpcid and username = ?\", userName)\r\nif err != nil {\r\nklog.Error(\"query orders count error\", err)\r\nreturn nil, err\r\n}\r\ntotal := 0\r\nfor totalRow.Next() {\r\nerr := totalRow.Scan(\r\n&total,\r\n)\r\nif err != nil {\r\nklog.Error(\"query orders count error\", err)\r\ncontinue\r\n}\r\n}\r\ntotalRow.Close()\r\n \r\nrows, err := s.Db.Query(SearchAllByUserNameSql, userName, query.Pagination.Limit, query.Pagination.Offset)\r\ndefer func() {\r\nif rows != nil {\r\nrows.Close()\r\n}\r\n}()\r\nif err != nil {\r\nklog.Error(\"query subnet error\", err)\r\nreturn nil, err\r\n}\r\nitems := make([]interface{}, 0)\r\nfor rows.Next() {\r\nss := new(vpc.SubnetItem)\r\nerr = rows.Scan(&ss.VpcName, &ss.VpcNetwork, &ss.SubnetName, &ss.SubnetNetwork, &ss.CreateTime)\r\nitems = append(items, *ss)\r\n}\r\nreturn &api.ListResult{\r\nTotalItems: total,\r\nItems: items,\r\n}, nil\r\n}\r\n\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\u7b2c\u4e00\u6b65\u67e5\u51fa\u4e86\u6240\u6709count\u6570\u76ee\uff1a
\n\"\"<\/p>\n

\u67e5\u8be2\u8bed\u53e5\u52a0\u4e0a\u53c2\u6570\u3002\u63a7\u5236\u67e5\u5230\u7684\u9875\u6570\u3002<\/p>\n

\"\"
\n\u8fd4\u56de\u7684\u662f\u603b\u6570\u91cf\u8ddf\u5f53\u524d\u67e5\u8be2\u9875\u3002<\/p>\n

\u7279\u6b8a\u60c5\u51b5\u7528\u4e0d\u5230\u6570\u636e\u5e93\u5206\u9875\u7684\uff0c\u5c31\u9700\u8981\u7528\u5230\u5185\u5b58\u5206\u9875\u4e86.<\/p>\n","protected":false},"excerpt":{"rendered":"

\/\/\u67e5\u8be2\u4e00\u4e2a\u7528\u6237\u4e0b\u6240\u6709\u7684subnet ws.Route(ws.GET(“\/subnets”). To(sc.L […]<\/p>\n","protected":false},"author":1903,"featured_media":239312,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239313"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239313\/revisions"}],"predecessor-version":[{"id":239319,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239313\/revisions\/239319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/239312"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}