2 java ebean playframework-2.0
我的项目中有一个模型类"Journey",它有几种删除,创建和列出所有旅程的方法.我正在使用heroku和postgresql数据库.我需要编写一个方法,它将返回所有具有与指定地址类似的地址的旅程.我知道查询结构通常是类似的SELECT address FROM Journey WHERE address ~~ arguement但我不知道在play框架中有什么功能可以做到这一点.
*public static void search(String address){
//query
//return matching journey results
}*
Run Code Online (Sandbox Code Playgroud)
您需要使用Model's 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;
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以轻松选择记录:
List<Journey> allJourneys = Journey.find.all();
List<Journey> searchedJourneys = Journey.find.where().like("address", "%foo%").findList();
Journey firstJourney = Journey.find.byId(123);
Run Code Online (Sandbox Code Playgroud)
在您的基本情况下,您可以将其添加到您的模型:
public static List<Journey> searchByAddress(String address){
return find.where().like("address", "%"+address+"%").findList();
}
Run Code Online (Sandbox Code Playgroud)
等它返回关系整个对象,所以在大数据集也可以是太重了,你可以甚至还应该使用更优化的查询与Finder的像链的方法select(),fetch()等指向你需要的时刻数据.
在Ebean的API中还有其他可能性,无论如何,您需要声明哪种方法最适合您.
顺便说一句,值得研究现有的示例应用程序,以便computer's database熟悉这个ORM.
编辑
对于情况insesitive搜索有更多的表现形式,即ilike(代替like)istartsWith,iendsWith,ieq,icontains和iexampleLike.它们在没有i开头的情况下做同样的版本.
您也可以在API中预览它们.