zoukankan      html  css  js  c++  java
  • List<实体>与List<String>数据互转

    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。

     

  • 相关阅读:
    Python中的unittest和logging
    android短彩信附件机制
    Android Mms之:深入MMS支持
    Android 源码阅读之SMS,MMS
    深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(一,添加附件)
    使用adb命令对手机进行截屏保存到电脑,SDCard
    Android中检测字符编码(GB2312,ASCII,UTF8,UNICODE,TOTAL——ENCODINGS)方法(二)
    解决新建短信时,输入“+86”,然后输入联系人名字“1”,按删除键之后,联系人变为“1”,删除操作为达到预期结果
    解决:People下面选择分享可见联系人,选择多个联系人后通过短信分享,短信中只显示一个联系人
    解决:短信添加录音附件,录音,没有录音时间限制,超出彩信最大限制也正常录音
  • 原文地址:https://www.cnblogs.com/goloving/p/7686341.html
Copyright © 2011-2022 走看看