问题描述
最近做员工考勤系统,现在有一个问题,就是返回经理所在部门所有员工的出勤情况,我现在已经能查询该经理对应的全部员工List<Employee> emps = empDao.findByMgr(m);也能够查询出员工的所有出勤情况Employee emp = empDao.findByName(empName);List<Attend> attends = attendDao.findByEmpAttend(emp);现在我遇到个问题,怎么把经理所在部门所有员工的出勤的list返回到页面我的思路是List<Employee> emps = empDao.findByMgr(m);System.out.println(emps); for(int i =0;i<emps.size();i++) { List<Attend> attends =attendDao.findByEmpAttend(emps.get(i)));}现在这样只能获取一个员工出勤的list,而我想把所有员工出勤的list,怎么做。
解决方案
1、List<Attend> attends = attendDao.findByEmpAttend(emps.get(i))); hibernate提供了in子查询List<Attend> attends = attendDao.findByEmpAttend(emps)); HQL可以通过setParameterList 设置in子查询这样全查回来再赋值2、写HQL,类似于select 员工,出勤 from 员工,出勤 where 员工.经理=? and 员工.id = 出勤.员工ID 自己转换3、原本在员工Employee对象中我有这样一个属性,private Set<Attend> attends = new HashSet<Attend>();你这个不是关系映射了吗? 直接关联抓不行? 最简单
解决方案二:
可以在员工Employee对象中再加个出勤list的属性,添加get和set方法;Employee{ private List<Attend> attends; ...... //get、set方法}然后遍历员工列表查询出勤list并set到员工对象中; List<Employee> emps = empDao.findByMgr(m); for(int i =0;i<emps.size();i++) { Employee emp = emps.get(i); List<Attend> attends =attendDao.findByEmpAttend(emp); emp.setAttends(attends);//set到员工对象中 } 在页面就可以两层循环遍历员工及员工的出勤list了
解决方案三:
public static void main(String[] args){List<Map> yitijiao = new ArrayList<Map>();// 未提交的员工List<Map> weitijiao = new ArrayList<Map>(); // 已提交的员工List<Map> weiwancheng = new ArrayList<Map>(); // 未完成的员工List<Map> bumenlist = new ArrayList<Map>();// 部门列表List<Map> jieguo = new ArrayList<Map>();Map<String, String> yuangong = new HashMap<String, String>();Map<String, String> bumen = new HashMap<String, String>();// ////////////部门////////////////////////bumen.put("id", "1");bumen.put("name", "事业部");bumenlist.add(bumen);bumen = new HashMap<String, String>();bumen.put("id", "2");bumen.put("name", "张三步");bumenlist.add(bumen);// /////////////员工/////////////////////// 已提交的yuangong.put("name", "张三");yuangong.put("id", "1");yuangong.put("bumenid", "1");yitijiao.add(yuangong);yuangong = new HashMap<String, String>();yuangong.put("name", "李四");yuangong.put("id", "2");yuangong.put("bumenid", "1");yitijiao.add(yuangong);yuangong = new HashMap<String, String>();yuangong.put("name", "王五");yuangong.put("id", "3");yuangong.put("bumenid", "2");yitijiao.add(yuangong);yuangong = new HashMap<String, String>();yuangong.put("name", "马六");yuangong.put("id", "4");yuangong.put("bumenid", "2");yitijiao.add(yuangong);// 未提交的yuangong = new HashMap<String, String>();yuangong.put("name", "冯七");yuangong.put("id", "6");yuangong.put("bumenid", "1");weitijiao.add(yuangong);yuangong = new HashMap<String, String>();yuangong.put("name", "房八");yuangong.put("id", "7");yuangong.put("bumenid", "2");weitijiao.add(yuangong);// 为完成的员工yuangong = new HashMap<String, String>();yuangong.put("name", "窜八");yuangong.put("id", "8");yuangong.put("bumenid", "1");weiwancheng.add(yuangong);yuangong = new HashMap<String, String>();yuangong.put("name", "阿九");yuangong.put("id", "9");yuangong.put("bumenid", "2");weiwancheng.add(yuangong);// 组合for (int i = 0; i < bumenlist.size(); i++){// 已提交的写到部门mapfor (int j = 0; j < yitijiao.size(); j++){if (yitijiao.get(j).get("bumenid").equals(bumenlist.get(i).get("id"))){jieguo.add(yitijiao.get(j));bumenlist.get(i).put("已提交", jieguo);}}jieguo = new ArrayList<Map>();// 未提交的写到部门mapfor (int j = 0; j < weitijiao.size(); j++){if (weitijiao.get(j).get("bumenid").equals(bumenlist.get(i).get("id"))){jieguo.add(weitijiao.get(j));bumenlist.get(i).put("未提交", jieguo);}}jieguo = new ArrayList<Map>();// 未完成的写到部门mapfor (int j = 0; j < weiwancheng.size(); j++){if (weiwancheng.get(j).get("bumenid").equals(bumenlist.get(i).get("id"))){jieguo.add(weiwancheng.get(j));bumenlist.get(i).put("未完成", jieguo);}}jieguo = new ArrayList<Map>();}// 解包for (int i = 0; i < bumenlist.size(); i++){System.out.println(bumenlist.get(i));/* * System.out.println("部门名称: " +bumenlist.get(i).get("name")); * System.out.println("部门ID: " +bumenlist.get(i).get("id")); List * temp = (List)bumenlist.get(i).get("已提交"); * System.out.println("已提交总数:" + temp.size()); for (int a = 0; a < * temp.size(); a++) { Map tt = (Map) temp.get(a); * System.out.println(tt.get("name") + "已提交"); } temp = * (List)bumenlist.get(i).get("未提交"); System.out.println("未提交总数:" + * temp.size()); for (int a = 0; a < temp.size(); a++) { Map tt = * (Map) temp.get(a); System.out.println(tt.get("name") + "未提交"); } * } */}}
解决方案四:
直接写关联sql
解决方案五:
主要看对象之间是怎么关联在一起的,如果有关联可以直接查出来的。Attend->Employee->Department,可能是这种关系吧如果是可以from Attent t where t.employee.department.code =?
解决方案六:
这个要看你表怎么设计的吧,如果出勤在员工表中,并且是bit之类的类型的话,直接sql语句就搞定啦
解决方案七:
List<Employee> emps = empDao.findByMgr(m); List<List<Attend>> lst = new ArrayList<List<Attend>>();System.out.println(emps); for(int i =0;i<emps.size();i++) { List<Attend> attends = attendDao.findByEmpAttend(emps.get(i)));lst.add(attends); }