zoukankan      html  css  js  c++  java
  • 常用HQL集锦

    1.根据ID查询某"一个"实体类

    方法1:

     String hql = "From ClassEntity as c where c.id=?";
     ClassEntity entity = (ClassEntity) getSession().createQuery(hql).setInteger(0, id).uniqueResult();

    方法2:

    ClassEntity entity=(ClassEntity) getSession().get(ClassEntity.class,id);

    2.获取"多个"实体类集合

    String hql = "From StudentEntity";
    List<StudentEntity> entityList=getSession().createQuery(hql).list();

    3.更新或者删除某个实体类

    例子1:

    String hql = "Delete StudentEntity as s where s.id=?";
    getSession().createQuery(hql).setInteger(0, id).executeUpdate();

    例子2:

    String hql = "Update StudentEntity as s set s.stuClass=?,s.stuName=?,s.birth=? where s.id=?";
    getSession().createQuery(hql).setEntity(0, studentEntity.getStuClass()).setString(1, studentEntity.getStuName()).
                 setDate(2, studentEntity.getBirth()).setInteger(3, studentEntity.getId()).executeUpdate();

    关于例子2的说明:

    setEntity():此方法可以直接赋值实体类。

    4.获得一个聚合函数的值

     String hql = "SELECT COUNT(*) FROM  StudentEntity";
     long count = (long) getSession().createQuery(hql).iterate().next();

     或者(推荐下面写法)

      String hql = "SELECT COUNT(*) FROM  StudentEntity";
      long count = (long) session.createQuery(hql).uniqueResult();

    5.新增一个数据

     getSession().saveOrUpdate(student);

    6.模糊查询

    String hql = "From StudentEntity as s where s.id= ? or s.stuName like ?";
    List<StudentEntity> lists = getSession().createQuery(hql).setString(0, "%" + param + "%").setString(1, "%" + param + "%").list();

    7.分页查询

    String hql = "From StudentEntity as s order by s.id desc ";
    List<StudentEntity> lists= getSession().createQuery(hql).setFirstResult((page - 1) * size).setMaxResults(size).list();

    8.多表联合查询(普通SQL不同)

    8.1 内连接

        String hql = "From StuInfo as stu Inner Join stu.stuClass as cla where cla.classId=?";
        List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
        //通过debug可以看出,此时返回值为一个object数组
        for (Object[] stuInfo : stuInfos) {
             System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
        }

    8.2 左外连接

        String hql = "From StuInfo as stu Left Join stu.stuClass as cla where cla.classId=?";
        List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
        //通过debug可以看出,此时返回值为一个object数组
        for (Object[] stuInfo : stuInfos) {
             System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
        }

    8.3 右外连接

        String hql = "From StuInfo as stu Right Join stu.stuClass as cla where cla.classId=?";
        List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
        //通过debug可以看出,此时返回值为一个object数组
        for (Object[] stuInfo : stuInfos) {
             System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
        }

    待明确:

    1.如果是3张表怎么处理?

    2.返回的对象debug截图。

    9.OR查询

            Query q = session.createQuery("from Dept d where deptId=:myId or deptName=:name");
            q.setParameter("myId", 12);
            q.setParameter("name", "财务部");

    10.范围查询

            Query q = session.createQuery("from Dept d where deptId between ? and ?");
            q.setParameter(0, 1);
            q.setParameter(1, 20);

    说明:

    1.利用between and 优于使用大于小于操作模式。

    11.

    总结:

    1.若返回的是一个list集合,那么使用list()。
    2.若返回的是一个集合,那么使用uniqueResult()。
    3.若只需要执行"改和删除",那么使用executeUpdate()。
    4.模糊查询需要在赋值的时候才加入%5.分页查询需要使用setFirstResult()和setMaxResults()。
  • 相关阅读:
    职业规划 !!
    linux上ssh配置指南
    低内存VPS用轻量级的Dropbear替换OpenSSH
    修改shell终端提示信息
    减少windows7内存占用的优化方案(内存占用才285兆 比XP还省)
    linux下提示符修改
    mysql存储过程学习笔记区块,条件,循环
    Apache下实现禁止目录浏览
    [学习指导] linux 启动过程以及 /etc/rc.d/init.d/目录的一点理解
    mysql 5.0存储过程学习总结
  • 原文地址:https://www.cnblogs.com/LiuChunfu/p/4936910.html
Copyright © 2011-2022 走看看