我只想检索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路线上的输出为空:(:
编辑:
如果我仅将
.iLike
更改为.eq
,则不会发生任何事情。将
.ilike
更改为.eq
并删除.findPagedList
的结果:最佳答案
您有5条记录,但是在对getPagedList
的调用中,您跳过了前25条记录。因此,您将跳过所有记录。
从Ebean documentation
参数:
pageIndex-页面的基于从零开始的索引。
pageSize-每页要返回的bean数。
因此,最初需要将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)
并使用
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));
}