用于过滤数据库记录的可选列 [英] Optional columns to filter dataBase records

查看:78
本文介绍了用于过滤数据库记录的可选列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用Ebean的finder创建带有可选列的查询来过滤某些数据?

How should I use the finder of Ebean to create a query with optional columns to filter some data?

我想获得一个List<User>.想象一下,我有3列要过滤:按名称,按类型和其他内容.如果选择了流派,则查询应列出所有选定流派的记录,此后,如果我选择名称John且流派为男性,则查询应列出所有"John"中的"male",依此类推.

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();

有什么建议吗?

推荐答案

尝试使用 ExpressionList .这是示例,如果if statement中的条件满足,则表达式将包含在where子句中.

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();

}

这篇关于用于过滤数据库记录的可选列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆