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数目:
查询语句加上参数。控制查到的页数。
返回的是总数量跟当前查询页。
特殊情况用不到数据库分页的,就需要用到内存分页了,下期小编给大家讲解内存分页。
本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ssfiction.com/sqljc/1073470.html