码迷,mamicode.com
首页 > 其他好文 > 详细

List<实体>与List<String>数据互转

时间:2017-10-18 15:00:30      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:except   from   数据   turn   常见   html   ide   query   server   

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<实体>与List<String>数据互转

标签:except   from   数据   turn   常见   html   ide   query   server   

原文地址:http://www.cnblogs.com/goloving/p/7686341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!