问题
How should I use the finder
of Ebean to create a query with optional columns to filter some data?
I want to get a List<User>
for instance. Imagine I have 3 columns to filter: by Name, by genre and something else. If I select the genre, the query should list all records of genre selected, after that, if I select name John and genre male, the query should List all "John" who are "male" and it's so on.
I know to create simple querys to find for a column:
find.where().eq("ex1",var1).eq("ex2", var2).findList();
Any suggestion ?
回答1:
Try using ExpressionList. Here is the sample, if the conditions in the if statement
met, then the expression will be included on the where clause.
public static List<User> filterUsers(int user_no, String genre, String name){
com.avaje.ebean.ExpressionList expressionList = find.where().eq("user_no", user_no);
if(condition1)
expressionList.eq("genre", genre);
if(condition2)
expressionList.eq("name", name);
return expressionList.findList();
}
来源:https://stackoverflow.com/questions/31534272/optional-columns-to-filter-database-records