• 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());
            }
        }
    

      

  • 相关阅读:
    操作系统——生产者消费者
    flutter如何搭建android环境
    小程序uni-app图片预览uni.previewImage会触发onshow这个生命周期
    小程序 uni-app动态更改标题
    小程序uni-app处理input框将页面往上推动的解决办法
    去除小程序scroll-view产生的横向滚动条
    小程序生命周期详解
    h5移动端像素适配 postcss-pxtorem和amfe-flexible
    vue平铺日历组件
    组合数
  • 原文地址:https://www.cnblogs.com/ruiannan/p/8119191.html
Copyright © 2020-2023  润新知