首页 > 代码库 > Hibernate框架查询方式
Hibernate框架查询方式
1. 唯一标识OID的检索方式 * session.get(对象.class,OID) 2. 对象的导航的方式
public class Demo { /** * 演示对象导航的方式 */ @Test public void run1() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); //先查询1号客户通过OID查询 Customer customer = session.get(Customer.class, 1L); //查询该客户下联系人集合大小 System.out.println(customer.getLinkmans().size()); tx.commit(); } /** * 查询联系人所属的客户 */ @Test public void run2() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); //查询2号联系人通过OID查询 Linkman linkman = session.get(Linkman.class, 2L); System.out.println(linkman.getCustomer().getCust_name()); tx.commit(); } }HQL方式的基本查询
/** * HQL方式的基本查询 * * @author bamaw */ public class Demo2 { /** * 基本查询 */ @Test public void run1() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); // 创建HQL的查询接口 Query query = session.createQuery("from Customer"); // 查询 //泛型写上,因为数据就封装进去了 List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 支持方法链编程 以及设置条件的查询 */ @Test public void run2() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); // 创建HQL的查询接口 /** * 如果设置条件 * session.createQuery("from Customer").setParameter(index,参数).list() * index表示参数第几位(从0开始) * 如:session.createQuery("from User where id = ? and name = ?") * id就是第0位 name第一位 */ List<Customer> list = session.createQuery("from Customer").list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 使用别名方式查询 * 注意:如果在HQL语句中这样写 * select * from Customer 语法错误 不能出现*号 * 这样我们可以通过起别名的方式 * 如:from Customer c * select c from Customer c; * */ @Test public void run3() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); List<Customer> list = session.createQuery("select c from Customer c ").list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 排序查询 * SQL语句中 order by 字段 asc升序(默认)/desc降序 * HQL语句中关键字是一样的 */ @Test public void run4() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); //我给起了个别名 还可以from Customer order by cust_id desc List<Customer> list = session.createQuery("from Customer c order by c.cust_id desc").list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 分页查询 mysql中 limit ?,? ---第一个?号 (当前页-1)*pageSize * 第二个?号 pageSize * setFirstResult(0); 值 = (当前页-1)*pageSize * setMaxResults(2); 每页查询的最大条数 */ @Test public void run5() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("from Customer"); query.setFirstResult(0);//从哪条记录开始,如果查询是从第一条开始,那么是0 /** * 查询第二页query.setFirstResult(3); * 值 = (当前页-1)*pageSize */ query.setMaxResults(3);//每页显示多少条 List<Customer> list = query.list(); //这里可以写成方法链的形式 //List<Customer> list = session.createQuery("from Customer").setFirstResult(0).setMaxResults(3).list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 按条件查询 */ @Test public void run6() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("from Customer where cust_id > ? and cust_level = ?"); /** * 我们以前用过 * Query query = session.createQuery("from Customer where cust_level = ?"); * 传入值 * query.setString(0,"高级"); * 还有一种方式 * Query query = session.createQuery("from Customer where cust_level = :cust_level"); * query.setString("cust_level","高级"); */ /** * setParameter("?号的位置,默认从0开始","参数的值"); 不用考虑参数的具体类型 * 按位置绑定参数的条件查询(指定下标值,默认从0开始) * 按名称绑定参数的条件查询(HQL语句中的 ? 号换成 :名称 的方式) */ query.setParameter(0, 2L); query.setParameter(1, "高级"); List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } }投影查询
/** * 我不想查所有字段,只查指定的几个字段, * 那么这个查询结果出来不能封装到对象当中的,这个时候就采用投影去查询 * 先演示为什么不能 * 返回的是对象数组 * 打印的结果 * Hibernate: * select * customer0_.cust_name as col_0_0_, * customer0_.cust_level as col_1_0_ * from * cst_customer customer0_ * [bamaw, 高级] * [巴莫, 初级] * 如果我就想把这些数据放到一个对象中,那么就new ,很麻烦 * 那么如果我就想返回对象,我们可以采用投影查询,它支持我们返回一个对象 */ @Test public void run7() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("select cust_name,cust_level from Customer"); List<Object[]> list = query.list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } tx.commit(); } /** * 投影查询 只查询个别属性 而不是全部 * 1.在JavaBean对象中 提供对应的构造方法(注意:要写空构造奥!!!) * 2.在HQL语句中发生变化 */ @Test public void run8() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("select new Customer(cust_name , cust_level) from Customer"); List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 聚合函数 count() sum() avg() count() max() min() * 在count(*) 中可以写* */ @Test public void run9() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); /** * 查询客户数量 * 我们查询过来得就是单列单行得数据并且是数值型 * 我们并不是都的拿对象封装 * 我们可以使用Number---Interger Long 的父类 */ List<Number> list = session.createQuery("select count(*) from Customer").list(); /** * 假如在sql语句中不写* * 我们可以"select count(l) from Linkman l" * 就是给表起一个别名 然后将别名引入,这样也是可以的 */ //通过下标取值并转化为相应类型的值 long count = list.get(0).longValue(); System.out.println(count); tx.commit(); } /**
* 获取某一列数据和 * 聚合函数:求数量和 sum(x) 里面传的是有关数值的字段 * 我表里面没有int类型 就拿cust_id算 */ @Test public void run10() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); List<Number> list = session.createQuery("select sum(cust_id) from Customer").list(); long sum = list.get(0).longValue(); System.out.println(sum); tx.commit(); }QBC查询方式:
/** * QBC查询 按条件查询(推荐) * @author bamaw * */ public class Demo3 { //简单的查询 @Test public void run1() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * QBC排序,调用方法 */ @Test public void run2() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); //调用排序方法addOrder() criteria.addOrder(Order.desc("cust_id"));//降序 List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * QBC分页 完全的面向对象 */ @Test public void run3() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.setFirstResult(0);//(当前页-1)*pageSize criteria.setMaxResults(3);//pageSize List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 条件查询(Criterion是查询条件的接口,Restrictions可不是criteria的实现类 ,而是 * Restrictions类是Hibernate框架提供的工具类,使用该工具类来设置查询条件) * 条件查询使用Criteria接口的add方法,用来传入条件。 * 使用Restrictions的添加条件的方法,来添加条件,例如: * Restrictions.eq -- 相等 * Restrictions.gt -- 大于号 * Restrictions.ge -- 大于等于 * Restrictions.lt -- 小于 * Restrictions.le -- 小于等于 * Restrictions.between -- 在之间 * Restrictions.like -- 模糊查询 * Restrictions.in -- 范围 * Restrictions.and -- 并且 * Restrictions.or -- 或者 */ @Test public void run4() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.eq("cust_level", "高级")); /** * 加入继续想加条件,可以继续在后面写 * criteria.add(Restrictions.gt("cust_id", 1L)); */ List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * in查询 */ @Test public void run5() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); //SQL:select * from cst_customer where cust_id in(1,2,6); 注意:::6不包含 List<Long> params = new ArrayList<Long>(); params.add(1L); params.add(2L); params.add(6L); //使用in查询 criteria.add(Restrictions.in("cust_id", params)); List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } } }/** * or查询 */ @Test public void run6() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.or(Restrictions.gt("cust_id", 1L), Restrictions.eq("cust_level", "初级"))); List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 判断某个字段为空的所有数据 * Restrictions.isNull("cust_phone") */ @Test public void run7() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); //查看在cust_phone为空的数据 criteria.add(Restrictions.isNull("cust_phone")); List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * QBC聚合函数的查询 */ @Test public void run8() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.count("cust_id")); List<Number> list = criteria.list(); long count = list.get(0).longValue(); System.out.println(count); tx.commit(); } /** * 强调问题:聚合函数的查询 * select count(lkm_id) from cst_linkman * 然后我们又想继续查select * from cst_linkman */ @Test public void run9() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.count("cust_id")); List<Number> list = criteria.list(); long value = http://www.mamicode.com/list.get(0).longValue(); System.out.println(value); //又想继续查select * from cst_linkman criteria.setProjection(null); List<Customer> list2 = criteria.list(); for (Customer customer : list2) { System.out.println(customer); } } /** * 统计查询 */ @Test public void run10() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.rowCount()); Long value = (Long) criteria.uniqueResult(); System.out.println(value); tx.commit(); }public class Demo4 { /** * QBC离线条件查询 * 离线条件查询使用的是DetachedCriteria接口进行查询, * 离线条件查询对象在创建的时候,不需要使用Session对象, * 只是在查询的时候使用Session对象即可。 */ @Test public void run() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); // 获得一个离线条件查询的对象 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class); detachedCriteria.add(Restrictions.eq("cust_level", "高级")); // 离线条件查询对象与session绑定. List<Customer> list = detachedCriteria.getExecutableCriteria(session).list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); } /** * 测试SQL语句的查询 */ @Test public void run2() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); //创建SQL查询接口 SQLQuery query = session.createSQLQuery("select * from cst_customer"); List<Object[]> list = query.list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } } /** * 如果要把数据封装到对象中 * 怎么办?? * 使用里面提供的方法 */ @Test public void run3() { Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); //创建SQL查询接口 SQLQuery query = session.createSQLQuery("select * from cst_customer"); List<Customer> list = query.addEntity(Customer.class).list(); for (Customer customer : list) { System.out.println(customer); } } }
Hibernate框架查询方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。