编程语言
首页 > 编程语言> > java-在Play Framework中使用WHERE子句返回列表

java-在Play Framework中使用WHERE子句返回列表

作者:互联网

我只想检索virtualfile等于我的virtualfile_id的记录(使用Ebean保留)

在路线http://localhost:9000/transactionRecords上,我有几个记录:

[{"id":1,"virtualfile":"1","record_type":"D","transaction_type":"MA","card_number":"0000122000200123456","expiry_date":"0000"},
{"id":2,"virtualfile":"1","record_type":"D","transaction_type":"MA","card_number":"0000122000200123456","expiry_date":"0000"},
{"id":3,"virtualfile":"1","record_type":"D","transaction_type":"MA","card_number":"0000122000200123456","expiry_date":"0000"},
{"id":4,"virtualfile":"1","record_type":"D","transaction_type":"MA","card_number":"0000122000200123456","expiry_date":"0000"},
{"id":5,"virtualfile":"1","record_type":"D","transaction_type":"MA","card_number":"0000122000200123456","expiry_date":"0000"}]

我获取它们的途径:

GET     /generator/:id             controllers.Application.generateFile(id: String)

我的控制器方法来获取它们:

public Result generateFile(String virtualfile_id) {
    List<TransactionRecord> transactionRecords = TransactionRecord.find.where()
            .ilike("virtualfile", "1") //set to "1" for testing
            .orderBy("id asc")
            .findPagedList(1, 25)
            .getList();
    return ok(toJson(transactionRecords));
}

*根据https://www.playframework.com/documentation/2.4.x/JavaEbean的建议获取列表的方法

还有我的TransactionRecord.class

@Entity
public class TransactionRecord extends Model {

  @Id
  Long id;
  String virtualfile;
  String record_type;
  String transaction_type;
  String card_number;
  String expiry_date;

  public static Finder<Long,TransactionRecord> find = new Finder<>(Long.class,TransactionRecord.class);

  public TransactionRecord(String virtualfile, String transaction_type, String card_number, String expiry_date) {
      this.virtualfile = virtualfile;
      this.record_type = "D";
      this.transaction_type = transaction_type;
      this.card_number = card_number;
      this.expiry_date = expiry_date;
  }

  getters & setters

}

但是我在路线http://localhost:9000/generator/1上的输出为空:(:

enter image description here

编辑:

如果仅将.iLike更改为.eq,则什么也不会发生.

将.ilike更改为.eq并删除.findPagedList的结果:

enter image description here

解决方法:

您有五个记录,但是在调用getPagedList时,您跳过了前25个记录.因此,您将跳过所有记录.

Ebean documentation开始

Parameters:
pageIndex – The zero based index of the page.
pageSize – The number of beans to return per page.

因此,最初需要将findPagedList 1的参数更改为0.

public Result generateFile(String virtualfile_id) {
    List<TransactionRecord> transactionRecords = TransactionRecord.find.where()
        .ilike("virtualfile", "1") //set to "1" for testing
        .orderBy("id asc")
        .findPagedList(0, 25)
        .getList();
    return ok(toJson(transactionRecords));
}

为了正确地支持客户端(或至少来自控制器层)的分页,您需要更改的签名.

public Result generateFile(String virtualfile_id)

public Result generateFile(String virtualfile_id, int page)

并使用该页面参数作为findPagedList的参数.

public Result generateFile(String virtualfile_id,
                           int page) {
    List<TransactionRecord> transactionRecords = TransactionRecord.find.where()
        .ilike("virtualfile", "1") //set to "1" for testing
        .order().asc("id")
        .findPagedList(page, 25)
        .getList();
    return ok(toJson(transactionRecords));
}

标签:playframework,playframework-2-4,java
来源: https://codeday.me/bug/20191119/2036961.html