把對象轉換為Map的Java工具庫:BeanQuery

jopen 10年前發布 | 26K 次閱讀 BeanQuery Java開發

BeanQuery 是一個把對象轉換為Map的Java工具庫。支持選擇Bean中的一些屬性,對結果進行排序和按照條件查詢。不僅僅可以作用于頂層對象,也可以作用于子對象。

BeanQuery的使用非常簡單也很直接,例子代碼如下:

//靜態導入BeanQuery
import static cn.jimmyshi.beanquery.BeanQuery.*;


//使用 select、from、where、orderBy、desc和asc來組裝一個Query,然后執行execute方法來獲得結果。
List<Map<String, Object>> result = select("price,name,mainAuthor.name as mainAuthorName")
    .from(bookCollection)
    .where(
        //for books name is Book2 or starts with Book1
        anyOf(
            value("name", startsWith("Book1")),
            value("name", is("Book2"))
        ),
        //for books price between (53,65)
        allOf(
            value("price", greaterThan(53d)),
            value("price",lessThan(65d))
        )
    )
    .orderBy("name").desc()
    .execute()

在上面的例子中,bookCollection的內容如下所示(json格式)

[
  {
    "price":55.55,
    "name":"Book1",
    "mainAuthor":{
      "name":"Book1-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518000"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  },
  {
    "price":52.55,
    "name":"Book12",
    "mainAuthor":{
      "name":"Book1-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518000"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  },
  {
    "price":53.55,
    "name":"Book13",
    "mainAuthor":{
      "name":"Book13-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518000"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  },
  {
    "price":60.0,
    "name":"Book14",
    "mainAuthor":{
      "name":"Book14-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518000"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  },
  {
    "price":50.55,
    "name":"Book15",
    "mainAuthor":{
      "name":"Book1-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518000"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  },
  {
    "price":77.77,
    "name":"Book3",
    "mainAuthor":{
      "name":"Book3-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518005"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  }
  ,
  {
    "price":66.66,
    "name":"Book2",
    "mainAuthor":{
      "name":"Book2-MainAuthor",
      "address":{
        "address":"Shenzhen Guangdong China",
        "postCode":"518005"
      },
      "birthDate":"1982-01-30T14:52:39"
    }
  }
]

執行完之后,則result的內容如下所示(json格式)

[
  {
    "price":60.0,
    "name":"Book14",
    "mainAuthorName":"Book14-MainAuthor"
  },
  {
    "price":53.55,
    "name":"Book13",
    "mainAuthorName":"Book13-MainAuthor"
  },
  {
    "price":55.55,
    "name":"Book1",
    "mainAuthorName":"Book1-MainAuthor"
  }
]

項目主頁:http://www.baiduhome.net/lib/view/home/1414554293059

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!