0

问题:大家好,我正在使用 Richfaces 开展一个项目,我想显示我的数据库(MySQL)的最后 10 行,这是我用来显示保存在我的数据库 CompanyBean.java 中的所有行的代码的一部分代码:

  public class EntrepriseBean implements Serializable{
private List<Entreprise> tableEntreprise ;
 
public List<Entreprise> getTableEntreprise() {
 
        ud= new EntrepriseDAO();
        tableEntreprise=ud.getAll();
 
        return tableEntreprise;
    }
 
    public void setTableEntreprise(List<Entreprise> tableEntreprise) {
        this.tableEntreprise = tableEntreprise;
    }
}

如果您有任何想法,它将对我非常有用。谢谢你。

4

1 回答 1

0

正如一些评论所指的那样,最后 10 的含义尚不清楚。在我看来,有两种很好的方法。在你的代码中,你可以做这样的事情

public List<Entreprise> getTableEntreprise() {

    ud = new EntrepriseDAO();
    tableEntreprise = ud.getAll();

    return tableEntreprise.stream().limit(10).collect(Collectors.toList());
}

另一种方法是更改​​ SQL 语句和使用限制,如下所示

SELECT * FROM enterprise
ORDER BY creation_time
LIMIT 10

这是假设 creation_time 是您表中的一个字段。或者,如果它是自动生成的,您可以按 id 排序,它会有更好的性能。

于 2021-05-11T13:00:30.680 回答