小编典典

java获得每个公司的平均薪水并传递给JSP

jsp

我试图获得每个公司的所有员工的平均工资:

<table>
    <tr>
        <th>Company</th>
        <th>Total Number of Employees</th>
        <th>Average Salary</th>
    </tr>
<% for (Company company : companys) {%>
    <tr>
        <td><%=company.getName()%></td>
        <td><%=company.getEmployees().size()%></td>
        <td><%=company.getEmployees() ???? %></td>              
    </tr>
<% } %>
</table>

我的问题:

我不知道如何计算每个公司的平均工资。 有人可以帮助我实现这一目标吗?

这是我的Company.java

public class Company implements Serializable {
...
    @OneToMany(mappedBy="company", fetch=FetchType.LAZY)
    private List<Employee> employees;

...
    public List<Employee> getEmployees() {
        return this.employees;
    }   
}

在我的controller.java中

private void doCompanys(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {      
    List<Company> companys = CompanyDAO.getAll();
    request.setAttribute("companys", companys);
    loadJSP(urlCompanys, request, response);
}

在我的CompanyDAO.java文件中

public static List<Company> getAll() {
    EntityManager em = GestionFactory.factory.createEntityManager();
    Query q = em.createQuery("SELECT c FROM Company c");
    @SuppressWarnings("unchecked")
    List<Company> listCompany = q.getResultList();
    return listCompany;
}

在我的EmployeeDAO.java中

public static List<Employee> getAll() {
        EntityManager em = GestionFactory.factory.createEntityManager();
        Query q = em.createQuery("SELECT e FROM Employee e");
        @SuppressWarnings("unchecked")
        List<Employee> listEmployee = q.getResultList();
        return listEmployee;
    }

在Employee.java中

public int getSalary() {
    return this.empSalary;
}

阅读 265

收藏
2020-06-10

共1个答案

小编典典

出于性能原因,应将平均计算委托给数据库。 避免CompanyDAO.getAll()自己计算平均值。这样做可能会导致N +
1个选择问题,你当LAZY装车选择。

使用此JPQL查询在数据库中进行平均值计算:

SELECT e.company, AVG(e.salary) FROM Employee e GROUP BY e.company

更准确地说,更改CompanyDAO.java如下:

public static List<Company> getAll() {
    EntityManager em = GestionFactory.factory.createEntityManager();
    Query q = em.createQuery("SELECT e.company, AVG(e.salary) FROM Employee e GROUP BY e.company");

    // each list entry contains a tuple (company, avgSalary)
    List<Object[]> results = q.getResultList();

    List<Company> companiesList = new LinkedList<>();
    for (Object[] entry : results) {
        Company company = (Company) entry[0]; // contains "e.company"
        Double avgSalary = (Double) entry[1]; // contains "AVG(e.salary)"
        company.setAverageSalary(avgSalary);
        companiesList.add(company);
    }

    return companiesList;
}

为此,请扩展您的Company.java实体类型,如下所示:

@Entity
public class Company implements Serializable {

    // ...

    @Transient
    private double averageSalary;

    public void setAverageSalary(double averageSalary) {
        this.averageSalary = averageSalary;
    }

    public double getAverageSalary() {
        return averageSalary;
    }
}

在您的JSP中,只需使用<%=company.getAverageSalary()%>

请考虑这是一个需要进一步完善的解决方案草案。 该解决方案适用于EclipseLink。对于Hibernate,您宁愿使用@Formula注释。

2020-06-10