hibernate单表查询总结(下)

hibernate单表查询总结(下)
本博客接着hibernate单表查询总结(上)继续就Hibernate的单表查询进行总结

一、分页查询

public void testQuerySingleTable_HQL_PagingPuery() {
    Session session = HbnUtils.getSession();
    try {
        session.beginTransaction();

        String hql = "from Student";
        int pageNo = 2;//当前页码
        int pageSize = 3;//每页记录数
        int firstResult =(pageNo - 1) * pageSize;//起始处
        List list = session.createQuery(hql)
        .setFirstResult(firstResult)
        .setMaxResults(pageSize)
        .list();

        for (Student stu : list) {
            System.out.println(stu);
        }
        session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

二、模糊查询

public void testQuerySingleTable_HQL_Vague() {
        Session session = HbnUtils.getSession();
        try {
            session.beginTransaction();

            String hql = "from Student where name like:name";
            List list = session.createQuery(hql)
                                                        .setString("name", "%张%")
                                                        .list();

            for (Student stu : list) {
                System.out.println(stu);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

三、聚合函数查询

public void testQuerySingleTable_HQL_AggregateFunction() {
        Session session = HbnUtils.getSession();
        try {
            session.beginTransaction();

            String hql = "select count(id) from Student";
            Long count = (Long) session.createQuery(hql).uniqueResult();

                System.out.println("一共有:"+count+"条数据");
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

四、映射查询

(只取某几个字段)

public void testQuerySingleTable_HQL_Mapping() {
        Session session = HbnUtils.getSession();
        try {
            session.beginTransaction();

            String hql = "select new Student(name,age) from Student";
            // 执行操作
            List list = session.createQuery(hql)
                    .list();

            for (Student stu : list) {
            System.out.println(stu);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

五、分组查询

public void testQuerySingleTable_HQL_Group() {
        Session session = HbnUtils.getSession();
        try {
            session.beginTransaction();
            //查询有几个年龄段
            String hql = "select age from Student gruoup by age";
            // 执行操作
            List list = session.createQuery(hql).list();
            // 执行操作
            for (Integer age : list) {
            System.out.println(age);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

六、命名查询

命名查询是将HQL语句配置到xml文件中,从而提高代码的可维护性,HQL语句可以配置到当前应用的任一xxx.hbm.xml文件中
hibernate单表查询总结(下)_第1张图片
具体位置是在class标签后面
hibernate单表查询总结(下)_第2张图片

public void testQuerySingleTable_HQL_NamedQuery() {
        Session session = HbnUtils.getSession();
        try {
            session.beginTransaction();
            //getNamedQuery("selectById")参数为配置文件里写的名字
            Student stu = (Student) session.getNamedQuery("selectById").setInteger("id", 3).uniqueResult();
            System.out.println(stu);
            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

七、关于单表查询的效率问题

在Query接口的List方法和Iterate方法总结一文中有详细讲述

今天分享的内容就是这些,如有疑问加群:511906138 大家一起讨论!

你可能感兴趣的:(java框架,Hibernate)