1、List<实体>数据:
public List<Device> queryOSDevice(String cpu,String ip,String name){ String sql = null; if(cpu.equals("os_xp")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') "; }else if(cpu.equals("os_7")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows7') "; }else if(cpu.equals("Os_Win8")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') "; }else if(cpu.equals("Os_Win10")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') "; }else if(cpu.equals("os_vista")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') "; }else if(cpu.equals("os_2003")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') "; }else if(cpu.equals("os_98")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') "; }else if(cpu.equals("os_95")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') "; }else if(cpu.equals("os_me")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') "; }else if(cpu.equals("os_nt")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') "; }else if(cpu.equals("os_2000")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') "; } if(!"".equals(ip) && !"".equals(name)){ sql += "and this.ip =:ip and this.name = :name "; return getSession().createQuery(sql) .setParameter("ip", ip) .setParameter("name", name) .list(); } if(!"".equals(ip) && "".equals(name)){ sql += "and this.ip =:ip "; return getSession().createQuery(sql) .setParameter("ip", ip) .list(); } if("".equals(ip) && !"".equals(name)){ sql += "and this.name = :name "; return getSession().createQuery(sql) .setParameter("name", name) .list(); } return getSession().createQuery(sql).list(); }
2、List<String>数据:
public List<String> queryOSDevice(String cpu,String ip,String name){ String sql = null; List<String> osDevice = new ArrayList<String>(); if(cpu.equals("os_xp")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') "; }else if(cpu.equals("os_7")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows7') "; }else if(cpu.equals("Os_Win8")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') "; }else if(cpu.equals("Os_Win10")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') "; }else if(cpu.equals("os_vista")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') "; }else if(cpu.equals("os_2003")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') "; }else if(cpu.equals("os_98")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') "; }else if(cpu.equals("os_95")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') "; }else if(cpu.equals("os_me")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') "; }else if(cpu.equals("os_nt")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') "; }else if(cpu.equals("os_2000")){ sql = "select this.ip,this.mac,this.ipNumber,this.name,this.devOnlyId,this.cpuType,this.memorySize,this.diskSize from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') "; } if(!"".equals(ip)){ sql += "and this.ip = '" + ip +"'"; } if(!"".equals(name)){ sql += "and this.name = '" + name + "'"; } osDevice = getSession().createQuery(sql).list(); return osDevice; }
注意:List<String>里面标红处需要注意的项,看下面这篇博客:常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could not locate named parameter错误的解决
List<Object>:
public List<Object> queryCountByOsAndOrgname(String cpu,String orgName) { List<Object> list = new ArrayList<Object>(); String sql = null; if(cpu.equals("os_xp")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') "; }else if(cpu.equals("os_7")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows7') "; }else if(cpu.equals("os_win8")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') "; }else if(cpu.equals("os_win10")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') "; }else if(cpu.equals("os_vista")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') "; }else if(cpu.equals("os_2003")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') "; }else if(cpu.equals("os_98")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') "; }else if(cpu.equals("os_95")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') "; }else if(cpu.equals("os_me")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') "; }else if(cpu.equals("os_nt")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') "; }else if(cpu.equals("os_2000")){ sql = "select count(*) from " + this.clazz.getName() + " this where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') "; } if(!"".equals(orgName)){ sql += "and this.organization.name = '" + orgName + "'"; } Number number = (Number)getSession().createQuery(sql).uniqueResult(); if(number==null){ number = 0; } JSONObject jsonObject = new JSONObject(); jsonObject.put(orgName, number); list.add(jsonObject); return list; }
List<Object>数据需要改一下:就是根据os查出来所有的数据,然后再根据orgName去分组,需要各orgName分组的数目;需要传递过去的接口数据类型如下:
[{"classid":"姚佳豪","value":1}, {"classid":"测试一部","value":1}, {"classid":"王明明","value":3}, {"classid":"陈锋","value":2}, {"classid":"黄睿","value":1}]
根据数据库查出来的数据如下:
@SuppressWarnings("unchecked") public List<Object> queryCountByOsAndOrgname(String cpu,Integer orgName) { List<Object> list = new ArrayList<Object>(); String sql = null; if(cpu.equals("os_xp")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') "; }else if(cpu.equals("os_7")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where d.osId = (select id from cems_dict_os o where o.name = 'com.vrv.common.system.os.Windows7') "; }else if(cpu.equals("os_win8")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') "; }else if(cpu.equals("os_win10")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') "; }else if(cpu.equals("os_vista")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') "; }else if(cpu.equals("os_2003")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') "; }else if(cpu.equals("os_98")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') "; }else if(cpu.equals("os_95")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') "; }else if(cpu.equals("os_me")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') "; }else if(cpu.equals("os_nt")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') "; }else if(cpu.equals("os_2000")){ sql = "select count(*),c.name from cems_device d,cems_organization c "
+ "where this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') "; } if(-1 == orgName){ sql += "and d.organizationId = c.id group by d.organizationId "; } List<Object[]> listobj = getSession().createSQLQuery(sql).list(); //注意List里面是Object[]对象数组的类型,这样下面listobj.get(i)[1],才可以获取到
for(int i = 0; i < listobj.size(); i++){ JSONObject jsonObject = new JSONObject(); jsonObject.put("classid", listobj.get(i)[1]); jsonObject.put("value", listobj.get(i)[0]); list.add(jsonObject); } return list; }
3、List<Device>实体数据:
public List<Device> queryOSDevice(String cpu,String ip,String name,String orgname){ String sql = null; List<Device> osDevice = new ArrayList<Device>(); if(cpu.equals("os_xp")){ sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') "; }else if(cpu.equals("os_7")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows7') "; }else if(cpu.equals("Os_Win8")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') "; }else if(cpu.equals("Os_Win10")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') "; }else if(cpu.equals("os_vista")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') "; }else if(cpu.equals("os_2003")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') "; }else if(cpu.equals("os_98")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') "; }else if(cpu.equals("os_95")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') "; }else if(cpu.equals("os_me")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') "; }else if(cpu.equals("os_nt")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') "; }else if(cpu.equals("os_2000")){ sql = " from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') "; } if(!"".equals(ip)){ sql += "and this.ip = '" + ip +"'"; } if(!"".equals(name)){ sql += "and this.name = '" + name + "'"; } if(!"".equals(orgname)){ sql += "and this.organization.name = '" + orgname + "'"; } osDevice = getSession().createQuery(sql).list(); return osDevice; }
表里关联的是:
映射XML文件:
<!-- 所属组织机构 -->
<many-to-one name="organization" class="com.vrv.cems.mgr.domain.Organization" not-null="true" column="organizationId" not-found="ignore"/>
这种写法:this.organization.name,能取到organization表里的name。