首页 > 其他 > 详细

oData

时间:2017-06-16 14:42:38      阅读:359      评论:0      收藏:0      [点我收藏+]

开放数据协议(OData)是一个查询和更新数据的Web协议。OData应用了web技术如HTTP、Atom发布协议(AtomPub)和JSON等来提供对不同应用程序,服务和存储的信息访问。除了提供一些基本的操作(像增删改查),也提供了一些高级的操作类似过滤数据和实体的导航。OData扩展了上述的协议但是不是取代他们。他可以被XML(ATOM)或者JSON取代但是OData的重要在于它符合REST原则。在某种意义上,它建立在‘简单‘的REST HTTP 服务上,并且有着清晰的目标——简化和标准化我们操作和查询数据的方式。如果你过去在给你的REST服务创建搜索、过滤、或者分页API的时候感觉很麻烦,那么OData将是一个不错的选择。

接口调用说明:

下表列举了一些常用的Odata操作:

操作

URL

说明

             $filter http://localhost:8090/api/Meetings?$filter=ProductName eq ‘Tofu‘ 根据表达式的状态返回结果(返回ProductName 等于Tofu的Products)
            $orderby http://localhost:8090/api/Meetings?$orderby=ProductName 根据结果排序(根据ProductName列排序)
            $skip http://localhost:8090/api/Meetings?$skip=10 越过结果中的n条数据,常用于分页
            $top http://localhost:8090/api/Meetings?$top=10 返回结果中的前n条记录,常用于分页
           $select http://localhost:8090/api/Meetings?$filter=ProductName eq ‘Tofu‘&$select=ProductName,UnitPrice 选择需要返回的属性
           $expand http://localhost:8090/api/Meetings?$expand=Supplier 返回Products中包含的导航属性(关联属性)Supplier
          $inlinecount http://localhost:8090/api/Meetings?$inlinecount=allpages 向服务器获取符合条件的资源总数(分页的total值)

通过上面表格的内容,我们还可以通过组合查询条件来实现复杂的查询。

常用查询举例:

  示例1:列出所有Product
  URL:http://localhost:8914/Products

   示例2,查询Products,只列出Name,Price例
  URL:http://localhost:8914/Products?$select=Name,Price

  示例3:列出Products(只有列Name,Price),包括Supplier
  URL:http://localhost:8914/Products?$select=Name,Price&$expand=Supplier

  示例4:过滤Products,只显示分类为Test的数据
  URL:http://localhost:8914/Products?$filter=Category eq ’Test‘

  示例5:过滤Products,只显示分类为Test的数据,并排序
  URL:http://localhost:8914/Products?$filter=Category eq ’Test‘&$orderby=Price desc

  $filter的其它的使用方式:

  1.  http://localhost/Products?$filter=Category eq ‘Test‘
    过滤Category=Test
  2.http://localhost/Products?$filter=Price lt 10
    过滤Price小于10
  3.http://localhost/Products?$filter=Price ge 5 and Price le 15
    过滤5<=Price>=15
  4.还可以使用数据库函数如:
    $filter=substringof(‘zz‘,Name)
    $filter=year(ReleaseDate) gt 2005

  5.关于排序:
    $orderby=Price
    $orderby=Price desc
    $orderby=Category,Price desc

  6.还有一些过滤器如:
    $skip,$top,$inlinecount等等

oData

原文:http://www.cnblogs.com/ellie-test/p/7027462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!