您的位置:首页 > 数据库

Hibernate直接执行sql语句,查询记录的个数

2011-05-31 14:31 393 查看
今天项目要用到查询数据库中满足某些条件的记录的个数,Hibernate中提供了Hql查询,但是我感觉那样查询效率不是很高,感觉用类似于hql查询的sql查询效率可能会更高,代码如下:

public int getMarkTimes(String ID, String goods_id) {
String hqlString = "select count(*) from Marks where ID=? and goods_id=?";
Session session = null;
try {
session = sf.openSession();
int count =((Long) session.createQuery(hqlString).setParameter(0, ID).setParameter(1, goods_id).iterate().next()).intValue();
System.out.println(count);
return count;
} catch(Exception e) {
e.printStackTrace();
return 0;
} finally {
if(session != null) session.close();
}
}

很多人感觉有疑问的地方可能就是这条语句select count(*) from Marks where ID=? and goods_id=?,其中Marks是一个类,它映射的数据库中的表中包含有ID和goods_id字段,这样就能满足我的需求
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: