标签:level 复杂 联想 ram 使用 hibernate rom xxx 有一个
注意:HQL语言中不会出现SQL中的表和列,HQL使用的都是JavaBean的类名和属性名。
(1)查询所有的基本语句
@Test // HQL查询所有数据 public void fun1() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 String hql = "from cn.qlq.domain.Customer";// from 类名全路径 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); // 4.根据查询对象获取查询结果 List<Customer> customers = query.list(); System.out.println(customers); }
结果:
Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_name as cust_nam2_0_, customer0_.cust_source as cust_sou3_0_, customer0_.cust_industry as cust_ind4_0_, customer0_.cust_level as cust_lev5_0_, customer0_.cust_linkman as cust_lin6_0_, customer0_.cust_phone as cust_pho7_0_, customer0_.cust_mobile as cust_mob8_0_ from cst_customer customer0_ [Customer [cust_id=1, cust_name=XXXXXXXXXX], Customer [cust_id=2, cust_name=联想]]
改进:如果整个项目中只有一个类的名字可以省略包路径,也就是可以只写类名:
@Test // HQL查询所有数据 public void fun1() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 // String hql = "from cn.qlq.domain.Customer";// from 类名全路径 String hql = "from Customer";// 如果整个项目中只有这一个类名可以直接写名字 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); // 4.根据查询对象获取查询结果 List<Customer> customers = query.list(); System.out.println(customers); }
(2)根据主键查询单个
@Test // HQL查询单个数据 public void fun2() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 // String hql = "from cn.qlq.domain.Customer";// from 类名全路径 String hql = "from Customer where cust_id = 1";// where后面是Customer的属性名称而不是列名 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); // 4.根据查询对象获取查询结果 Customer customer = (Customer) query.uniqueResult(); System.out.println(customer); }
(1)?占位符查询
类似于JDBC的占位符,只是hibernate的?下标从0开始,而JDBC的下标从1开始,基本上所有的编程索引都从0开始,唯独JDBC从1开始。。。。
@Test // HQL的?占位符查询 public void fun3() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 // String hql = "from cn.qlq.domain.Customer";// from 类名全路径 String hql = "from Customer where cust_id = ?";// 如果整个项目中只有这一个类名可以直接写名字 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); // query.setLong(0, 1l);//类似于JDBC的占位符,只是JDBC的占位符下标从0开始,hibernate从1开始 query.setParameter(0, 1l);//这种写法不用管类型 // 4.根据查询对象获取查询结果 Customer customer = (Customer) query.uniqueResult(); System.out.println(customer); }
(2)命令占位符 :name格式的查询,固定格式,name随便起,习惯性的起做和条件名字一样
@Test // HQL的命令占位符查询 public void fun4() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 // String hql = "from cn.qlq.domain.Customer";// from 类名全路径 String hql = "from Customer where cust_id = :cust_id";// :cust_id的名字随便起,只不过习惯性的起做一样 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); // query.setLong(0, 1l);//类似于JDBC的占位符,只是JDBC的占位符下标从0开始,hibernate从1开始 query.setParameter("cust_id",1l); // 4.根据查询对象获取查询结果 Customer customer = (Customer) query.uniqueResult(); System.out.println(customer); }
分页查询类似于mysql的limit关键字,limit start,pageSize。。。。。。
@Test // HQL分页查询 public void fun5() { // 1 获得session Session session = HibernateUtil.openSession(); // 2.书写HQL语句 // String hql = "from cn.qlq.domain.Customer";// from 类名全路径 String hql = "from Customer";// :cust_id的名字随便起,只不过习惯性的起做一样 // 3.根据hql创建查询对象 Query query = session.createQuery(hql); /** * 类似于 limit start,pageSize; * 假设页大小是2 * 页号 起始值 页大小 * 1 0 2 * 2 2 2 */ //例如取第二页数据 query.setFirstResult(2); query.setMaxResults(2); // 4.根据查询对象获取查询结果 List<Customer> customers = query.list(); System.out.println(customers); }
结果:
Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_name as cust_nam2_0_, customer0_.cust_source as cust_sou3_0_, customer0_.cust_industry as cust_ind4_0_, customer0_.cust_level as cust_lev5_0_, customer0_.cust_linkman as cust_lin6_0_, customer0_.cust_phone as cust_pho7_0_, customer0_.cust_mobile as cust_mob8_0_ from cst_customer customer0_ limit ?, ? [Customer [cust_id=3, cust_name=博客园], Customer [cust_id=4, cust_name=仍然]]
标签:level 复杂 联想 ram 使用 hibernate rom xxx 有一个
原文地址:https://www.cnblogs.com/qlqwjy/p/9357369.html