假设我有以下代码:
String sql = "select c.id, c.name from Company c left join Tag t on c.id=t.company_id where c.state='PUBLISHED'";
List<SqlRow> sqlRows =
Ebean.createSqlQuery(sql).setMaxRows(4000000).findList();
for(SqlRow row : sqlRows) {
// Do cool stuff
}
findList()
然后将所有内容加载到内存中,这不是我在这种情况下真正想要的内容。
我看到Query<T>
有findIterate()
但Ebean.createSqlQuery(sql)
返回SqlQuery
但没有findIterate()
。那我该怎么做?
答案 0 :(得分:2)
我猜RFTM就在这里,或者花5分钟然后回到问题所在。 我就这样做了:
<强>服务类强>
String sql = "Select c.id, c.name from Company c left join Tag t on c.id=t.company_id where c.state='PUBLISHED'";
RawSql rawSql = RawSqlBuilder
.parse(sql)
.columnMapping("c.name", "word")
.columnMappingIgnore("c.id")
.create();
Query<SearchWord> query = Ebean.find(SearchWord.class);
QueryIterator<SearchWord> iterator = query
.setMaxRows(4000000)
.setRawSql(rawSql)
.findIterate();
try {
while (iterator.hasNext()) {
iterator.next()
// do magic stuff in here
}
} finally {
iterator.close();
}
<强>搜索内容强>
@Entity
public class SearchWord {
private String word;
private String type;
public SearchWord() {
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}