Optional columns to filter dataBase records

匆匆过客 提交于 2019-12-11 11:14:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!