Ebean 只选择特定的列

Fai*_*dul 1 playframework ebean

我有一个带有字段 id 、 name 、 price 等的类产品,...

我只想从表中获取名称..

我目前正在使用此查询

 String sql =   "select name from product where price = 100";
 SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
  List<SqlRow> list = sqlQuery.findList();
Run Code Online (Sandbox Code Playgroud)

使用 List 查找但仅获取名称的警报方式是什么

List<product> list = product.find.where("price = 100").select("name").findList();
Run Code Online (Sandbox Code Playgroud)

我认为以下查询效率不高,因为它获取所有数据并返回我们对其进行过滤的情况

  List<String> list = new ArrayList<String>();

    for(product p: product.find.select("name").findList())
     {
        list.add(p.name);
    }


return list;
Run Code Online (Sandbox Code Playgroud)

Rob*_*ave 5

Ebean 有findSingleAttributeList()findSingleAtrribute()所以替代答案是:

List<String> productNames =
  product.find
    .where().eq("price", 100)
    .select("name")
    .findSingleAttributeList();
Run Code Online (Sandbox Code Playgroud)