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

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

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

  1. List list = getHibernateTemplate()
  2. .find(
  3. "select o.id from  SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime",
  4. new Object[] { bussNo, typePath, "1" });

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

  1. List list = (List) getHibernateTemplate().execute(
  2. new HibernateCallback() {
  3. public Object doInHibernate(Session session)
  4. throws HibernateException, SQLException {
  5. Query query = session.createQuery("select o.id from  SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
  6. query.setParameter(0, bussNo);
  7. query.setParameter(1, typePath);
  8. query.setParameter(2, "1");
  9. return query.list();
  10. }
  11. });

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

  1. List list = (List) getHibernateTemplate().executeWithNativeSession(
  2. new HibernateCallback() {
  3. public Object doInHibernate(Session session)
  4. throws HibernateException, SQLException {
  5. Query query = session
  6. .createQuery("select o.id from  SfmFileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
  7. query.setParameter(0, bussNo);
  8. query.setParameter(1, typePath);
  9. query.setParameter(2, "1");
  10. return query.list();
  11. }
  12. });

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

  1. List list = (List) getHibernateTemplate().execute(
  2. new HibernateCallback() {
  3. public Object doInHibernate(Session session)
  4. throws HibernateException, SQLException {
  5. SQLQuery query = session
  6. .createSQLQuery("select o.id from  Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
  7. query.setParameter(0, bussNo);
  8. query.setParameter(1, typePath);
  9. query.setParameter(2, "1");
  10. return query.list();
  11. }
  12. });

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

  1. List list = (List) getHibernateTemplate().executeWithNativeSession(
  2. new HibernateCallback() {
  3. public Object doInHibernate(Session session)
  4. throws HibernateException, SQLException {
  5. SQLQuery query = session
  6. .createSQLQuery("select o.id from  Sfm_FileIndex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
  7. query.setParameter(0, bussNo);
  8. query.setParameter(1, typePath);
  9. query.setParameter(2, "1");
  10. return query.list();
  11. }
  12. });

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

  1. PreparedStatement ps = getSession()
  2. .connection()
  3. .prepareStatement(
  4. "select o.id from sfm_fileindex o where o.bussNo = ? and o.typePath = ? and o.validStatus = ? order by o.uploadTime");
  5. ps.setString(1, bussNo);
  6. ps.setString(2, typePath);
  7. ps.setString(3, "1");
  8. ResultSet rs = ps.executeQuery();
  9. List list = new ArrayList();
  10. while (rs.next()) {
  11. list.add(new Long(rs.getLong(1)));
  12. }
  13. rs.close();
  14. ps.close();

Hibernate查询效率对比的更多相关文章

  1. EF 数据查询效率对比

    优化的地方: 原地址:https://www.cnblogs.com/yaopengfei/p/9226328.html ①:如果仅是查询数据,并不对数据进行增.删.改操作,查询数据的时候可以取消状态 ...

  2. WFS: postgresql(postgis)和shp文件查询效率对比

    对GeoServer上的WFS的各种数据源查询效率感兴趣,做个测试.本次测试了Postgresql.geopackage.shp文件三种数据源的查询效率,无论是本机还是服务器环境,pg存储查询效率都比 ...

  3. Redis查询&JDBC查询&Hibernate查询方式的效率比较...

    比较三种查询方式查询效率对比...我是用的JavaWeb的方式通过通过JSP页面查询的填写查询的参数...给予反馈.... 整个demo的下载地址:http://files.cnblogs.com/f ...

  4. 查询最新记录的sql语句效率对比

    在工作中,我们经常需要检索出最新条数据,能够实现该功能的sql语句很多,下面列举三个进行效率对比 本次实验的数据表中有55万条数据,以myql为例: 方式1: SELECT * FROM t_devi ...

  5. 【持久化框架】Mybatis与Hibernate的详细对比

        前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Myba ...

  6. 【持久化框架】Mybatis与Hibernate的详细对比(转发)

    前言 这篇博文我们重点分析一下Mybatis与Hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...

  7. Mybatis与Hibernate的详细对比

    前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...

  8. Hibernate查询

    HIbernate查询 使用get方法 使用get方法通过持久类名和ID号查找一个对象Stu instance = (Stu) getsession() .get("com.lovo.po. ...

  9. Hibernate查询之HQL查询

    转自:http://blog.csdn.net/xiao_yi/article/details/1733342 Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(H ...

随机推荐

  1. android studio获取SHA1

    1 打开cmd,转到路径:C:\Users\usoft\.android 2 输入命令 keytool -list -v -keystore debug.keystore 3 输入命令 android ...

  2. 用户输入 i. 检测常用手势(一)

    参考: http://blog.csdn.net/qq418716640/article/details/8508973http://www.cnblogs.com/mengdd/p/3335508. ...

  3. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  4. context:property-placeholder

    这个在spring中配置文件中是非常常用的. context:property-placeholder大大的方便了我们数据库的配置. 只需要在spring的配置文件里添加一句:<context: ...

  5. apache开源项目 -- Wicket

    [infoq] Apache Wicket是一个功能强大.基于组件的轻量级Web应用框架,能将展现和业务逻辑很好地分离开来.你能用它创建易于测试.调试和支持的高质量Web 2.0应用.假设其他团队交付 ...

  6. eclipse集承jboss服务器

    eclipse Kepler + Jboss7.1 参考引用文档: http://www.tekdigest.com/how-to-install-jboss-tools-in-eclipse.htm ...

  7. Android 项目利用 Android Studio 和 Gradle 打包多版本APK

    在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...

  8. html input readonly 和 disable的区别

    Readonly和Disabled它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / password)和textar ...

  9. 转载:Hadoop权威指南学习笔记

    转自:http://pieux.github.io/blog/2013-05-08-learn-hadoop-the-definitive-guide.html 1 前言 Hadoop的内部工作机制: ...

  10. 关于duilib中的list的扩展探索

    原文地址:http://blog.csdn.net/tragicguy/article/details/21893065 今天在做一个程序的界面时,需要在一个列表中显示除文字以外的其他控件,如:Edi ...