List<實體>與List數據互轉

一、List<實體>數據:html

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(); }

二、List<String>數據:sql

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>:json

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}]
數組

根據數據庫查出來的數據以下:app

@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; }

三、List<Device>實體數據:dom

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; }

  表裏關聯的是:ide

  映射XML文件:post

<!-- 所屬組織機構 -->
<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。測試

相關文章
相關標籤/搜索