String sql="";
String loginedUser=this.getUserService().getCount("User", sql);//总注册人数
request.getSession().setAttribute("loginedUser", loginedUser);

String month=Util.getCurrentDate("yyyyMM");
sql="where regDate like  '%"+month+"%'";
String loginedMonth=this.getUserService().getCount("User", sql);//本月注册人数
request.getSession().setAttribute("loginedMonth", loginedMonth);

String today=Util.getCurrentDate("yyyyMMdd");
sql="where lastLoginDate like  '%"+today+"%'";
String todayVisit=this.getUserService().getCount("User", sql);//今日会员访问人数
request.getSession().setAttribute("todayVisit", todayVisit);看上面的代码,都是要查询User表中不同条件下的人数,这样的话没执行一次getCount()方法就要链接一次数据库,效率不高,能不能写的简化一些,使得效率高一些。getCount()方法如下:
public String getCount(String tableName, String condition) {
String hql = "select count(*) from " + tableName + " " + condition;
String tmp = this.getSession().createQuery(hql).list().get(0).toString();
return tmp;
}

解决方案 »

  1.   

    肯定是要查数据库的,关键是怎么优化sql查询。
    比如本月注册用户,你可以专门建个表
    month  |   count  | year
      1          100     2009有一个注册,更新下。查询的时候直接查询这个就可以了,是不是比每次都
    select count(*) 效率要高点呢
      

  2.   


    每次查询的时候,不必重新连接数据库。yes,就是这个意思,阁下有啥办法哇啦?
      

  3.   

    试一下 左外连接 left outer join  ,如果用到框架了,还可以延迟检索
      

  4.   


    左外连接 left outer join  好像是在不同的表中用的吧?!