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。

List<实体>与List<String>数据互转的更多相关文章

  1. 基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据

    最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框架保持一致,而在Web上,我主要采用EasyUI的前端界面处理技术,走MVC的技术路线,在重 ...

  2. 自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)

    /// <summary> /// 获取请求参数字段 /// </summary> /// <typeparam name="T"></t ...

  3. 使用Json实体类构建菜单数据

    基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框 ...

  4. Json、JavaBean、String等互转

    Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...

  5. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  6. 匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

    [100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型 问题描述如下:我有一个方法如下:public IQueryable Dissen ...

  7. Mybatis中实体类属性和数据列之间映射的四种办法

    http://blog.csdn.net/lmy86263/article/details/53150091 Mybatis不像hibernate中那么自动化,通过@Column注解或者直接使用实体类 ...

  8. (转)基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据

    http://www.cnblogs.com/wuhuacong/p/3669708.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...

  9. C# 实体类转json数据过滤掉字段为null的字段

    C# 实体类转json数据过滤掉字段为null的字段 语法如下: var jsonSetting = new JsonSerializerSettings {NullValueHandling = N ...

随机推荐

  1. Django-urls路由分发

      例如:127.0.0.0:8000/blog/lucaq.html,当有多个应用时,需要在blog应用下进行路由,我们在blog应用下做一个urls路由分发,就需要include模块实现.   导 ...

  2. UVALIVE 3891 The Teacher's Side of Math

    One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial ...

  3. gcc 簡單操作

    gcc -c test.c 產出 test.o object file gcc -c test.c -o XXX 產出 XXX object file gcc test.c -o aaa 產出 aaa ...

  4. 超详细saltstack安装部署及应用

    1.环境准备 准备两台虚拟机 主机名 ip role linux-node1 10.0.0.7 master linux-node2 10.0.0.8 minion 在节点1上安装 master 和 ...

  5. js的变量的有效域

    function test(o) { var i=0; if(typeof o=="object") { var j=1; for(var k=0;k<10;k++) { c ...

  6. Python 进阶 之 协程

    协程的概念级描述(与线程对比):转自知乎 链接 线程有两个必须要处理的问题:一是碰着阻塞式I\O会导致整个进程被挂起: 二是由于缺乏时钟阻塞,进程需要自己拥有调度线程的能力. 如果一种实现使得每个线程 ...

  7. Go简单的Goroutine示例

    最简单的,接下来,会是竞争,加锁... package main import ( "fmt" "runtime" "sync" ) var ...

  8. logging 日志两种使用方法(转)

    下面我们使用代码logging的代码来说明: 使用baseConfig()函数对 logging进行 简单的 配置: import logging; # 使用baseConfig()函数,可选参数有f ...

  9. keyPoints的相关函数

    cout<<"坐标:"<<keypoints1[i].pt; cout<<",邻域直径:"<<keypoints ...

  10. 11.5NOIP2018提高组模拟题

    书信(letter) Description 有 n 个小朋友, 编号为 1 到 n, 他们每人写了一封信, 放到了一个信箱里, 接下来每个人从中抽取一封书信. 显然, 这样一共有 n!种拿到书信的情 ...