简体   繁体   English

播放框架数据库搜索

[英]Play Framework Database Search

I have a model class "Journey" in my project which has several methods to delete, create and list all of the journeys. 我的项目中有一个“旅程”模型类,其中有几种删除,创建和列出所有旅程的方法。 I am using heroku and a postgresql database. 我正在使用heroku和一个postgresql数据库。 I need to write a method that will return all journeys that have a similar address to one specified. 我需要编写一种方法,该方法将返回所有与指定地址具有相似地址的旅程。 I know the query structure would typically be something like SELECT address FROM Journey WHERE address ~~ arguement but I don't know what functions exist to do this in the play framework. 我知道查询结构通常类似于SELECT address FROM Journey WHERE address ~~ arguement但我不知道在play框架中存在哪些函数可以执行此操作。

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

You need to use Model's Finder for an example: 您需要以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;

}

so you can easily select records with: 因此您可以轻松地通过以下方式选择记录:

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

In your base case you can add this to your model: 在基本情况下,您可以将其添加到模型中:

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

Etc. It returns whole objects with relations, so in big data sets it can be too heavy, you can or even should also use more optimized queries with Finder's chained methods like select() , fetch() etc to point which data you need at the moment. 等等,它返回具有关联关系的整个对象,因此在大数据集中它可能太重了,您甚至可以甚至通过Finder的链式方法(如select()fetch()select()使用更优化的查询来指出所需的数据此时此刻。

There are also other possibilities in Ebean's API , anyway you need to declare which approach is most optimal for you. Ebean的API中还存在其他可能性,无论如何,您都需要声明哪种方法最适合您。

BTW, it's worthy to examine existing sample applications, for an example computer's database to get familiar with this ORM. 顺便说一句,值得检查现有的示例应用程序,例如示例computer's database以熟悉此ORM。

Edit 编辑

For case insesitive searching there are additional Expressions ie ilike (instead of like ) , istartsWith , iendsWith , ieq , icontains and iexampleLike . 对于格格不入的搜索,还有其他表达式,即ilike (而不是like ), istartsWithiendsWithieqicontainsiexampleLike They does the same what version without i at the beginning. i开始没有i时,它们的版本相同。

You can preview them in the API as well. 您也可以在API中预览它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM