1.什么是hql
Hibernate Query Language即Hibernate查询语言,面向的是类和属性
下面开始列子
//1.HQL 检索所有学生集合 @org.junit.Test public void testselectAllDepts(){ String hql="from Student"; Query query = session.createQuery(hql); List<Student> list = query.list(); for (Student student:list) { System.out.println(student.getStuname()); } } //2.获取学生的信息 @org.junit.Test public void testselectSomeRows(){ String hql="from Student d where d.stuname='呵呵'"; Query query = session.createQuery(hql); List<Student> list = query.list(); for (Student student:list) { System.out.println(student.getStuname()); } } //3.获取部分列 @org.junit.Test public void testgetMultiColumns(){ String hql="select d.stuname from Student d"; Query query = session.createQuery(hql); List<String> list = query.list(); for (String dept:list) { System.out.println(dept); } } //3.获取部分列 多列 List<Objecgt[]> @Test public void testgetMultipleColumns(){ String hql="select d.stuname,d.sage from Student d"; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for (Object[] dept:list) { for (Object item:dept){ System.out.print(item+"==="); } System.out.println(); } }
1.匿名占位符
from Student where stuname = ?
2.名称占位符
from Student where s.stuname = :stuname
3.绑定命名参数与一个对象的属性值
Session session = HibernateUtil.openSession();
NewsDetail news=new NewsDetail();
news.setNtitle("hibernate");
hibernate 工具类
public class HibernateUtil { // 初始化一个ThreadLocal对象 @SuppressWarnings("rawtypes") private static final ThreadLocal sessionTL = new ThreadLocal(); private static Configuration configuration; private final static SessionFactory sessionFactory; static { try { configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session currentSession() { //sessionTL的get()方法根据当前线程返回其对应的线程内部变量, Session session = (Session) sessionTL.get(); // 如果session为null,则打开一个新的session if (session == null) { //创建一个数据库连接对象session。 session = sessionFactory.openSession(); // 保存该数据库连接session到ThreadLocal中。 sessionTL.set(session); } //sessionTL中get()可以获取该线程上次获取过的数据库连接对象。 return session; } /** * 关闭Session */ public static void closeSession(){ Session session = (Session) sessionTL.get(); sessionTL.set(null); session.close(); }
hql实现动态查询
1.检索条件的实体属性
//job private String job; //salary private Double sal; //入职开始时间 private Date fromDate; //入职结束时间 private Date endDate;
2.测试
@Test public void test05() throws ParseException { /*准备对象*/ Electronic ex=new Electronic(); /*价格小于等于800.0*/ ex.setPrice(800.0); /*准备hql,hql根据条件动态生成*/ StringBuilder stringBuilder=new StringBuilder("from Electronic e where 1=1 "); /*判断价格是否为空*/ if(ex.getPrice()!=null){ stringBuilder.append("and e.price<=:price"); } /*3. 依据hql构建query对象*/ Query query=session.createQuery(stringBuilder.toString()); /*使用query对象的setProperties()方法为参数赋值,empCondition对象中封装了条件*/ query.setProperties(ex); List<Electronic> list = query.list(); /*遍历*/ for (Electronic item:list) { System.out.println(item.getEid()+"==="+item.getE_code()+"==="+item.getPrice()); } }
分页查询
uniqueResult()获取唯一对象
setFirstResult(int firstResult) 设置返回结果从第几条开始 ---- 索引从0开始
setMaxResults(int maxResults) 设置本次返回结果记录条数
@Test public void selectPageEc(){ String hql="from Electronic"; Query query = session.createQuery(hql); int pageSize=3; int pageIndex=1; query.setFirstResult((pageIndex-1)*pageSize); //6 query.setMaxResults(pageSize); List<Electronic> list = query.list(); for (Electronic item:list) { System.out.println(item.getE_code()); } }