hibernate 的 HQL学习笔记

HQL之占位查询:

1.方案一:?匿名占位符

 @Test
    public void test02(){

        String hql="from Dept d where d.dname=? and d.loc=?";
        Query query=session.createQuery(hql);
        query.setParameter(0,"SALES");
        query.setParameter(1,"CHICAGO");
        List<Dept> list=query.list();
        for (Dept item:list) {
            System.out.println(item.getDname());
        }
    }

2.方案二:name 参数名称绑定

@Test
    public void test03(){

        String hql="from Dept d where d.dname=:dname and d.loc=:loc";
        Query query=session.createQuery(hql);
        query.setParameter("dname","SALES");
        query.setParameter("loc","CHICAGO");
        List<Dept> list=query.list();
        for (Dept item:list) {
            System.out.println(item.getDname());
        }
    }

 3.方案三:name 参数名称绑定+++对象属性

@Test
    public void test04(){

        String hql="from Dept d where d.dname=:dname and d.loc=:loc";
        Query query=session.createQuery(hql);

        DeptModel deptModel=new DeptModel();
        deptModel.setDname("SALES");
        deptModel.setLoc("CHICAGO");
        query.setProperties(deptModel);
        List<Dept> list=query.list();
        for (Dept item:list) {
            System.out.println(item.getDname());
        }
    }

  

HQL语句之动态查询:

//1.动态查询
    @Test
    public void test01() throws ParseException {
        EmpCondition emp=new EmpCondition();
        //伪造界面的Condition
        //视图model
        emp.setJob("CLERK");
        emp.setSal(1000.0);
        emp.setFromDate(Tool.strToDate("1980-04-01"));
        emp.setEndDate(new Date());


        //根据条件拼接sql
        StringBuilder sb=new StringBuilder("from Emp e where 1=1");
        if (emp.getJob()!=null){
            sb.append("and e.job=:job");
        }
        if (emp.getSal()!=null){
            sb.append("and e.sal>:sal");
        }
        if (emp.getFromDate()!=null){
            sb.append("and e.hiredate>=:fromDate");
        }
        if (emp.getEndDate()!=null){
            sb.append("and e.hiredate<=:endDate");
        }

        //Query query1 = HibernateUtil.getSession().createQuery(sb.toString());

        Query query=session.createQuery(sb.toString());
        query.setProperties(emp);
        List<Emp> list = query.list();
        for (Emp item:list) {
            System.out.println(item.getEname());
        }


    }

HQL语句之util工具:

  //线程变量
    static ThreadLocal<Session> tlSession=new ThreadLocal<Session>();

    //SessionFactory
    public static SessionFactory factory;
    static Configuration cfg=null;
    static {
        cfg=new Configuration().configure();
        factory=cfg.buildSessionFactory();
    }

    //01.获取链接
    public static Session getSession(){
        Session session=tlSession.get();
        if (session==null){
            session=factory.openSession();
            tlSession.set(session);
        }
        return session;
    }
    //02.释放链接
    public static void closeSession(){
        Session session=tlSession.get();
        if (session!=null){
            //线程变量set成null
            tlSession.set(null);
            session.close();
        }
    }

  

HQL语句之分页:

/**
     * 分页
     */
    @Test
    public void selectPage(){
        String hql="from Emp order by empno";
        Query query = session.createQuery(hql);
        int pageIndex=1;
        int pageSize=3;
        query.setFirstResult((pageIndex-1)*pageSize);
        query.setMaxResults(pageSize);
        List<Emp> empList=query.list();
        for (Emp emp:empList){
            System.out.println(emp.getEname());
        }
    }

  

 

posted @ 2017-12-26 17:36  哈喽小伙  阅读(248)  评论(0编辑  收藏  举报