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 = ;
}
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(- == 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 = ; i < listobj.size(); i++){
JSONObject jsonObject = new JSONObject();
jsonObject.put("classid", listobj.get(i)[]);
jsonObject.put("value", listobj.get(i)[]);
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。

最新文章

  1. Fibonacci 1
  2. POJ 1273 Drainage Ditches -dinic
  3. JAVA设计模式--State(状态模式)
  4. bzoj3208:花神的秒题计划I
  5. C语言的本质(34)——静态库
  6. LintCode-字符串查找
  7. TestNG--入门介绍教程
  8. Android源码解析——LruCache
  9. 关于android app签名文件获取sha1和MD值
  10. Java数组的应用:案例:杨辉三角,三维数组,字符串数组
  11. BZOJ_3173_[Tjoi2013]最长上升子序列_splay
  12. Message高级特性 &amp; 内嵌Jetty实现文件服务器
  13. CatBoost算法和调参
  14. redis集群(jedis)批量删除同一前缀
  15. 剑指offer(28)数组中出现次数超过一半的数
  16. WebService简单教程
  17. bash shell中可以使用wait
  18. Beta阶段——Scrum 冲刺博客第一天
  19. Spring EL运算符实例
  20. SSIS -&gt;&gt; 管理和维护SSISDB

热门文章

  1. wxpython SizerItem的大小控制
  2. Makefile 的 prequisite 執行順序 single multi thread
  3. (十一)__LINE__、__FUNCTION__的使用
  4. PL/SQL 04 游标 cursor
  5. [ 总结 ] Linux系统测试硬盘I/O
  6. 第一篇:Hello World
  7. redis发布订阅、HyperLogLog与GEO功能的介绍
  8. Windows下python的第三方库的安装
  9. HDU 2552 三足鼎立(数学函数)
  10. 浅谈Java中final,finalized,finally