首页 > 代码库 > Hibernate SQLQuery 原生SQL 查询及返回结果集处理-2
Hibernate SQLQuery 原生SQL 查询及返回结果集处理-2
1.
返回List,
.setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);将结果转为Map,存放到list中,即list中为若干map对象;
获取可按此方法:
for (int i = 0; i < list.size(); i++) {
Map map = (Map) list.get(i);
System.out.println(map.get("table_colum_name"));
}
Map map = (Map) list.get(i);
System.out.println(map.get("table_colum_name"));
}
在jsp页面,可用S标签获取
<s:property value="http://www.mamicode.com/table_colum_name" />
public List getDamTestType(Integer damId) throws Exception {
List list = new ArrayList();
Session session = null;
Transaction tx = null;
String strSql1 = "";
strSql1 = "select distinct testType.id,testType.mc from [" + damId + "_lb] as testType "
+ "inner join [" + damId + "_jcxm] as testObj on testObj.lbgl_id=testType.id "
+ "inner join [" + damId + "_cd] as testSite on testSite.jcxm_id=testObj.id "
+ "inner join [" + damId + "_sctj] as upLoadCond on upLoadCond.cd_id=testSite.id ";
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Query query = session.createSQLQuery(strSql1).setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);
list = query.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
throw e;
} finally {
session.close();
}
return list;
}
List list = new ArrayList();
Session session = null;
Transaction tx = null;
String strSql1 = "";
strSql1 = "select distinct testType.id,testType.mc from [" + damId + "_lb] as testType "
+ "inner join [" + damId + "_jcxm] as testObj on testObj.lbgl_id=testType.id "
+ "inner join [" + damId + "_cd] as testSite on testSite.jcxm_id=testObj.id "
+ "inner join [" + damId + "_sctj] as upLoadCond on upLoadCond.cd_id=testSite.id ";
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Query query = session.createSQLQuery(strSql1).setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);
list = query.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
throw e;
} finally {
session.close();
}
return list;
}
Hibernate SQLQuery 原生SQL 查询及返回结果集处理-2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。