簡體   English   中英

播放框架數據庫搜索

[英]Play Framework Database Search

我的項目中有一個“旅程”模型類,其中有幾種刪除,創建和列出所有旅程的方法。 我正在使用heroku和一個postgresql數據庫。 我需要編寫一種方法,該方法將返回所有與指定地址具有相似地址的旅程。 我知道查詢結構通常類似於SELECT address FROM Journey WHERE address ~~ arguement但我不知道在play框架中存在哪些函數可以執行此操作。

*public static void search(String address){
//query
//return matching journey results
}*

您需要以Model的Finder為例:

package models;

import play.db.ebean.Model;

import javax.persistence.*;

@Entity
public class Journey extends Model {

    @Id
    public Integer id;

    public static Finder<Integer, Journey> find
            = new Model.Finder<>(Integer.class, Journey.class);

    // other fields
    public String address;
    public String country;

}

因此您可以輕松地通過以下方式選擇記錄:

List<Journey> allJourneys = Journey.find.all();
List<Journey> searchedJourneys = Journey.find.where().like("address", "%foo%").findList();
Journey firstJourney = Journey.find.byId(123);

在基本情況下,您可以將其添加到模型中:

public static List<Journey> searchByAddress(String address){
    return find.where().like("address", "%"+address+"%").findList();
}

等等,它返回具有關聯關系的整個對象,因此在大數據集中它可能太重了,您甚至可以甚至通過Finder的鏈式方法(如select()fetch()select()使用更優化的查詢來指出所需的數據此時此刻。

Ebean的API中還存在其他可能性,無論如何,您都需要聲明哪種方法最適合您。

順便說一句,值得檢查現有的示例應用程序,例如示例computer's database以熟悉此ORM。

編輯

對於格格不入的搜索,還有其他表達式,即ilike (而不是like ), istartsWithiendsWithieqicontainsiexampleLike i開始沒有i時,它們的版本相同。

您也可以在API中預覽它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM