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

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

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

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

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

viewplaincopytoclipboardprint?

01.Listlist=getHibernateTemplate()

02..find(

03."selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime",

04.newObject[]{bussNo,typePath,"1"});

Listlist=getHibernateTemplate()

.find(

"selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime",

newObject[]{bussNo,typePath,"1"});

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

viewplaincopytoclipboardprint?

01.Listlist=(List)getHibernateTemplate().execute(

02.newHibernateCallback(){

03.publicObjectdoInHibernate(Sessionsession)

04.throwsHibernateException,SQLException{

05.Queryquery=session.createQuery("selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

06.query.setParameter(0,bussNo);

07.query.setParameter(1,typePath);

08.query.setParameter(2,"1");

09.returnquery.list();

10.}

11.});

Listlist=(List)getHibernateTemplate().execute(

newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)

throwsHibernateException,SQLException{

Queryquery=session.createQuery("selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

query.setParameter(0,bussNo);

query.setParameter(1,typePath);

query.setParameter(2,"1");

returnquery.list();

}

});

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

viewplaincopytoclipboardprint?

01.Listlist=(List)getHibernateTemplate().executeWithNativeSession(

02.newHibernateCallback(){

03.publicObjectdoInHibernate(Sessionsession)

04.throwsHibernateException,SQLException{

05.Queryquery=session

06..createQuery("selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

07.query.setParameter(0,bussNo);

08.query.setParameter(1,typePath);

09.query.setParameter(2,"1");

10.returnquery.list();

11.}

12.});

Listlist=(List)getHibernateTemplate().executeWithNativeSession(

newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)

throwsHibernateException,SQLException{

Queryquery=session

.createQuery("selecto.idfromSfmFileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

query.setParameter(0,bussNo);

query.setParameter(1,typePath);

query.setParameter(2,"1");

returnquery.list();

}

});

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

viewplaincopytoclipboardprint?

01.Listlist=(List)getHibernateTemplate().execute(

02.newHibernateCallback(){

03.publicObjectdoInHibernate(Sessionsession)

04.throwsHibernateException,SQLException{

05.SQLQueryquery=session

06..createSQLQuery("selecto.idfromSfm_FileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

07.query.setParameter(0,bussNo);

08.query.setParameter(1,typePath);

09.query.setParameter(2,"1");

10.returnquery.list();

11.}

12.});

Listlist=(List)getHibernateTemplate().execute(

newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)

throwsHibernateException,SQLException{

SQLQueryquery=session

.createSQLQuery("selecto.idfromSfm_FileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

query.setParameter(0,bussNo);

query.setParameter(1,typePath);

query.setParameter(2,"1");

returnquery.list();

}

});

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

viewplaincopytoclipboardprint?

01.Listlist=(List)getHibernateTemplate().executeWithNativeSession(

02.newHibernateCallback(){

03.publicObjectdoInHibernate(Sessionsession)

04.throwsHibernateException,SQLException{

05.SQLQueryquery=session

06..createSQLQuery("selecto.idfromSfm_FileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

07.query.setParameter(0,bussNo);

08.query.setParameter(1,typePath);

09.query.setParameter(2,"1");

10.returnquery.list();

11.}

12.});

Listlist=(List)getHibernateTemplate().executeWithNativeSession(

newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)

throwsHibernateException,SQLException{

SQLQueryquery=session

.createSQLQuery("selecto.idfromSfm_FileIndexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

query.setParameter(0,bussNo);

query.setParameter(1,typePath);

query.setParameter(2,"1");

returnquery.list();

}

});

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

viewplaincopytoclipboardprint?

01.PreparedStatementps=getSession()

02..connection()

03..prepareStatement(

04."selecto.idfromsfm_fileindexowhereo.bussNo=?ando.typePath=?ando.validStatus=?orderbyo.uploadTime");

05.ps.setString(1,bussNo);

06.ps.setString(2,typePath);

07.ps.setString(3,"1");

08.ResultSetrs=ps.executeQuery();

09.Listlist=newArrayList();

10.while(rs.next()){

11.list.add(newLong(rs.getLong(1)));

12.}

13.rs.close();

14.ps.close();

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