Hibernate中的createQuery查询一条数据、多条数据、分页查询数据
2017-10-01 12:28
459 查看
package com.ckinghan.test; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; import com.ckinghan.bean.User; public class HibernateQueryApi { /** * Hibernate 中的createQuery查询一条数据、多条数据、分页查询数据 */ @Test public void hibernateQuery1(){ //加载Hibernate的配置文件信息 Configuration configuration = new Configuration().configure(); //通过配置文件信息创建SessionFactory SessionFactory buildSessionFactory = configuration.buildSessionFactory(); //通过SessionFactory获取与当前线程绑定的Session Session currentSession = buildSessionFactory.getCurrentSession(); //对Session启动事务 Transaction transaction = currentSession.beginTransaction(); //创建HQL的查询语句 Query createQuery = currentSession.createQuery("from User where id = 1"); //获取一条数据,但要注意,如果查询返回的结果集是多条数据,会报错,如果未查询到数据,返回null User user = (User) createQuery.uniqueResult(); System.out.println("查询一条数据:"); System.out.println(user); //获取多条数据,不管是调用uniqueResult()方法时还是调用list方法,都会重新执行一次SQL查询语句 List<User> users = (List<User>) createQuery.list(); System.out.println("查询多条数据:"); System.out.println(users); //分页功能,设置分页的起始索引 createQuery.setFirstResult(0); //设置分页每次获取的数据数量 createQuery.setMaxResults(100); //查询数据 createQuery = currentSession.createQuery("from User"); List<User> list = createQuery.list(); //输出查询到的结果集 System.out.println("分页查询数据:"); System.out.println(list); //提交事务 transaction.commit(); //关闭SessionFactory buildSessionFactory.close(); } }
执行结果 如下:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_ where user0_.id=1 查询一条数据: User [id=1, userName=userName, password=password123] Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_ where user0_.id=1 查询多条数据: [User [id=1, userName=userName, password=password123]] Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_ 分页查询数据: [User [id=1, userName=userName, password=password123], User [id=2, userName=userName, password=password123]]
相关文章推荐
- hibernate 查询两个时间段的数据和分页实现
- hibernate分页查询,在自己写的s2sh注册登录的基础上添加对数据表的分页显示功能
- hibernate使用createQuery(hql)语句仅查询部分字段,应如何获得数据
- 学会hibernate数据查询分页
- 一条SQL查询单个数据和多个数据,并能在分页上显示
- 遇见了奇怪bug,前端用的AngularJs,后台是SpringMVC,持久用的hibernate,跟后台断点时查询数据库返回一条数据,否则返回两条,以下是问题详述
- hibernate_查询简介: 分页, 数据总数, 结果集的包装, 位置参数的设置
- hibernate_数据分页, 查询总数
- Hibernate 分页中,查询指定页面的数据
- Hibernate基础:快速入门(7):查询数据
- 带条件分页查询的主数据列表(施工单位为例)
- 在Java的Hibernate框架中对数据库数据进行查询操作
- 纳税服务系统【条件查询数据回显、分页】
- Hibernate分页查询小结
- 查询数据的最大排序问题(只能用一条语句写)
- hibernate之查询(给结果进行分页)
- Hibernate的增删查改(分页+其他查询)
- 解决几百万条以上数据分页让人蛋疼的 SQL2005, SQL2008最后一页卡死,最后一页查询超时的源码
- 查询离指定日期最近的一条数据(oracle)
- Linq 分页查询数据