Hibernate查询的各种方式效率比较

[url]http://blog.csdn.net/zhouxianli/archive/2010/01/25/5253063.aspx[/url]


查询已知表名的实体时推荐使用getHibernateTemplate().executeWithNativeSession() + SQLQuery方式。

以下测试使用JUnit进行,仅查询一次,查询结果为5条记录。各种方式的详细代码及执行时间如下所示:

方式1,正常getHibernateTemplate().find()方式(183ms):

view plaincopy to clipboardprint?
01.List list = getHibernateTemplate()
02..find(
03."select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime",
04.new Object[] { bussNo, typePath, "1" });
List list = getHibernateTemplate()
.find(
"select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime",
new Object[] { bussNo, typePath, "1" });

方式2,使用getHibernateTemplate().execute() + Query方式(214ms):

view plaincopy to clipboardprint?
01.List list = (List) getHibernateTemplate().execute(
02. new HibernateCallback() {
03. public Object doInHibernate(Session session)
04. throws HibernateException, SQLException {
05. Query query = session.createQuery("select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
06. query.setParameter(0, bussNo);
07. query.setParameter(1, typePath);
08. query.setParameter(2, "1");
09. return query.list();
10. }
11. });
List list = (List) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
query.setParameter(0, bussNo);
query.setParameter(1, typePath);
query.setParameter(2, "1");
return query.list();
}
});

方式3,使用getHibernateTemplate().executeWithNativeSession() + Query方式(184ms):

view plaincopy to clipboardprint?
01.List list = (List) getHibernateTemplate().executeWithNativeSession(
02. new HibernateCallback() {
03. public Object doInHibernate(Session session)
04. throws HibernateException, SQLException {
05. Query query = session
06. .createQuery("select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
07. query.setParameter(0, bussNo);
08. query.setParameter(1, typePath);
09. query.setParameter(2, "1");
10. return query.list();
11. }
12. });
List list = (List) getHibernateTemplate().executeWithNativeSession(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session
.createQuery("select o.id from SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
query.setParameter(0, bussNo);
query.setParameter(1, typePath);
query.setParameter(2, "1");
return query.list();
}
});

方式4,使用getHibernateTemplate().execute() + SQLQuery方式(102ms):

view plaincopy to clipboardprint?
01.List list = (List) getHibernateTemplate().execute(
02. new HibernateCallback() {
03. public Object doInHibernate(Session session)
04. throws HibernateException, SQLException {
05. SQLQuery query = session
06. .createSQLQuery("select o.id from Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
07. query.setParameter(0, bussNo);
08. query.setParameter(1, typePath);
09. query.setParameter(2, "1");
10. return query.list();
11. }
12. });
List list = (List) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery query = session
.createSQLQuery("select o.id from Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
query.setParameter(0, bussNo);
query.setParameter(1, typePath);
query.setParameter(2, "1");
return query.list();
}
});

方式5,使用getHibernateTemplate().executeWithNativeSession() + SQLQuery方式(68ms):

view plaincopy to clipboardprint?
01.List list = (List) getHibernateTemplate().executeWithNativeSession(
02. new HibernateCallback() {
03. public Object doInHibernate(Session session)
04. throws HibernateException, SQLException {
05. SQLQuery query = session
06. .createSQLQuery("select o.id from Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
07. query.setParameter(0, bussNo);
08. query.setParameter(1, typePath);
09. query.setParameter(2, "1");
10. return query.list();
11. }
12. });
List list = (List) getHibernateTemplate().executeWithNativeSession(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery query = session
.createSQLQuery("select o.id from Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
query.setParameter(0, bussNo);
query.setParameter(1, typePath);
query.setParameter(2, "1");
return query.list();
}
});

方式6,使用JDBC (用于比较,代码不够健壮)(37ms):

view plaincopy to clipboardprint?
01.PreparedStatement ps = getSession()
02..connection()
03..prepareStatement(
04."select o.id from sfm_fileindex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
05.ps.setString(1, bussNo);
06.ps.setString(2, typePath);
07.ps.setString(3, "1");
08.ResultSet rs = ps.executeQuery();
09.List list = new ArrayList();
10.while (rs.next()) {
11.list.add(new Long(rs.getLong(1)));
12.}
13.rs.close();
14.ps.close();


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhouxianli/archive/2010/01/25/5253063.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值