循环中调用DAO时解决方案。(数据量比较大时应该采用的方法。)

来源:互联网 发布:意式 咖啡机推荐 知乎 编辑:程序博客网 时间:2024/05/21 00:01

在页面中显示的某个字段(A)是数据库中查出来的字段(B)和另一个查出来的字段(C)相关操作之后得出来的。

查出来的字段(B)是根据(C)的某个条件查出来的。

这时候不可以在(C)这个字段中循环使用C的某个字段为条件来查询出来B这个字段。

解决的方法:

在写B的方法是,查询条件和C的查询条件一直,然后相关联相关表 同时把 B中的相关字段查询出来存到集合中(DataSet,Set, arrayList或者HashMap都可以。)

public static  HashMap<Object, Object> findNotifyTotalDuebillBalance(Database db,
       String pcCode, String districtCode, String ccCode,
       String studentName,String studentCode) throws Exception
  {
    String sql = "select d.studentCode,sum(duebillBalance) as totalDuebillBalance " +
           "\n from duebill d" +
           "\n left join StudentRelation sr on sr.studentCode=d.studentCode" +
           "\n left join CountyCenter cc on cc.ccCode=sr.ccCode" +
           "\n where 1=1";
   
    if(!ccCode.equals(""))
        sql += "\n and sr.ccCode='"+ccCode+"'";
      else if(!districtCode.equals(""))
        sql += "\n and   exists (select 1 from CountyCenter cc where cc.ccCode=dd.deductCcCode and cc.pccCode like '"+districtCode+"%')";
      else if(!pcCode.equals(""))
        sql += "\n and sr.pcCode='"+pcCode+"'";
      if(!studentName.equals(""))
        sql += "\n and sr.studentName like '"+studentName+"%'";
      if(!studentCode.equals(""))
          sql += "\n and sr.studentCode = '"+studentCode+"'";
     
      sql += "\n group by d.studentCode";
     
    //执行查询
    DataSet ds = db.executeQuery(sql);
    HashMap<Object, Object> duebillMap = new HashMap<Object, Object>();
    for (int i=0;i<ds.size();i++)
    {
     duebillMap.put(ds.getString(i, "studentCode"),ds.getDouble(i, "totalDuebillBalance"));
    }
    return duebillMap;
  }

项目中的数据比较特殊是double类型的,存的时候可以转成double类型的封装类存起来,取的时候需要使用

String dubeill= duebillMap.get(cEnNotifyStudent.getStudentCode()).toString();
e_hj=Double.parseDouble(dubeill);

需要转换一下取出来。

 

批量的时候,调用 findNotifyTotalDuebillBalance方法时,传入和查询条件一致的参数,但是不传入studentCode,单个时,前面4个参数传入“”,空就可以了。

批量时根据查询出来的studentCode的hashMap就可以查找出对应的values值。

就不需要在循环中,根据每次循环的studentCode来查找对应的values值了。

 

 

原创粉丝点击