• hibernate的查询方法以及返回的值类型


    Hibernate的查询分几种
    HQL,QBC(QBE),SQL

    HQL查询
    HQL是基于面向对象的,里面没有表,也没有列,有类和属性
    直接以from开头,表示全部选择
    从Hibernate3.1可以执行修改和删除

    from Emp

    eg1.  带?参数查询
    Session session = HibernateSessionFactory.getSession();
    String hql="from Emp e where e.ename like ?";
    Query query=session.createQuery(hql);
    query.setString(0, "%"+"a"+"%");
    List list=query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg.2 带:命名参数
    Session session = HibernateSessionFactory.getSession();   
    String hql="from Emp e where e.ename like :name";
    Query query=session.createQuery(hql);
    query.setString("name", "%a%");
    List list=query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg3.in查询
    Session session = HibernateSessionFactory.getSession();
    String hql="from Emp e where e.empno in (:list)";
    Query query=session.createQuery(hql);
    query.setParameterList("list", new Long[]{1L,3L,5L});
    List list=query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg4.连接查询
    Session session = HibernateSessionFactory.getSession();
    String hql = "from Emp e where e.dept.dname like ?";
    Query query = session.createQuery(hql);
    query.setString(0, "%RE%");
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg5. 笛卡尔积
    Session session = HibernateSessionFactory.getSession();
    String hql = "select e from Emp e,Dept";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg6. 带条件
    Session session = HibernateSessionFactory.getSession();
    String hql = "select e from Emp e,Dept d where e.dept=d";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg7. select 多个对象,会形成 List里面放Object数组,数组里面放对象
    Session session = HibernateSessionFactory.getSession();
    String hql = "select e,d from Emp e,Dept d where e.dept=d";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg8. select 一列,就会形成这一列类型的集合,不会生成Object数组
    Session session = HibernateSessionFactory.getSession();
    String hql = "select e.empno from Emp e,Dept d where e.dept=d";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg9. select多个值,会形成Object[]
    Session session = HibernateSessionFactory.getSession();
    String hql = "select e.empno,e.ename from Emp e,Dept d where e.dept=d";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg10.查询生成List里面放HashMap,key由as后面的字符串决定
    Session session = HibernateSessionFactory.getSession();
    String hql = "select new map(e.empno as id,e.ename as name) from Emp e";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg11.迫切左连接查询
    Session session = HibernateSessionFactory.getSession();
    String hql = "from Emp e left join fetch e.dept";
    Query query = session.createQuery(hql);
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

    eg12.分页查询
    Session session = HibernateSessionFactory.getSession();
    String hql = "from Emp ";
    Query query = session.createQuery(hql);
    query.setFirstResult(10);//从第几条开始,初始是0  (page-1)*pageSize;
    query.setMaxResults(5);//取几条
    List list = query.list();
    System.out.println(list.size());
    HibernateSessionFactory.closeSession();

  • 相关阅读:
    怎么解决input中readonly属性的iOS一直存在光标问题
    点击图片播放视频
    h5上传图片
    添加和删除节点
    阻止事件冒泡
    四分之一圆border-radius的用法
    【记录一个问题】libtask无法在android下编译通过
    【记录一个问题】云风的协程库 c conroutine无法在android下链接通过
    【记录一个问题】macos下lldb调试opencv的一个程序,出现“failed to load objfile for”错误,并且无法调试进入opencv的函数
    【测试数据】android下CPU核与线程数的关系
  • 原文地址:https://www.cnblogs.com/lcuzhanglei/p/2629180.html
Copyright © 2020-2023  润新知