Java mac idea hibernate的使用04

1. HQL语法

1.1 基本语法

  1. 查全部
@Test
    public void testFunc01() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer";

        Query query = session.createQuery(hql);

        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 排序
        // 升序
        String hql = " from Customer order by cust_id asc ";

        // 降序
        String hql = " from Customer order by cust_id desc ";

  1. 条件查询
  // 条件查询
    @Test
    public void testFunc03() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer where cust_id = ?";

        Query query = session.createQuery(hql);

        query.setParameter(0, 3l);

        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
@Test
    public void testFunc04() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer where cust_id = :cust_id";

        Query query = session.createQuery(hql);

        query.setParameter("cust_id", 1l);

        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 分页查询
 // 分页
    @Test
    public void testFunc05() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer";
        
        Query query = session.createQuery(hql);
        // 相当于 sql 中的 limit ?, ?
        query.setFirstResult(1);
        query.setMaxResults(1);

        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 聚合查询
        // sum 总条数
        String hql = "select count(*) from Customer ";

        // 求和
        String hql = "select sum (cust_id) from Customer ";

        // 求平均值
        String hql = "select avg (cust_id) from Customer ";

        // 求最大值
        String hql = "select max (cust_id) from Customer ";

        // 求最小值
        String hql = "select min (cust_id) from Customer ";

        Query query = session.createQuery(hql);
        
        Number number = (Number) query.uniqueResult();

        System.out.println(number);
  1. 投影查询
// 投影查询
    @Test
    public void testFunc07() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " select cust_name from Customer ";

        Query query = session.createQuery(hql);


        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
@Test
    public void testFunc08() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " select new Customer(cust_id, cust_name)  from Customer ";

        Query query = session.createQuery(hql);

        List list = query.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }

1.2 HQL多表查询

  1. 内连接
// 内连接:将连接两端的对象分别返回,放到数组中
    @Test
    public void testFunc09() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer c inner join c.linkMen";

        Query query = session.createQuery(hql);

        List list = query.list();

        for (Object[] arr : list) {
            System.out.println(Arrays.toString(arr));
            System.out.println("---------");
        }

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 迫切内连接:帮我们进行封装,返回值就是一个对象
// 迫切内连接
    @Test
    public void testFunc10() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        String hql = " from Customer c inner join fetch c.linkMen";

        Query query = session.createQuery(hql);

        List list = query.list();

        for (Customer customer : list) {
            System.out.println(customer);
            System.out.println("---------");
        }

        transaction.commit();

        session.close();
    }
  1. 左外连接
@Test
    //HQL 左外连接 => 将连接的两端对象分别返回.放到数组中.
    public void testFunc11(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c left join c.linkMen ";

        Query query = session.createQuery(hql);

        List list = query.list();

        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }
  1. 右外连接
@Test
    //HQL 右外连接 => 将连接的两端对象分别返回.放到数组中.
    public void testFunc12(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c right join c.linkMen ";

        Query query = session.createQuery(hql);

        List list = query.list();

        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

2. Criteria查询(QBC)

  1. 基本查询
@Test
    public void testFunc01() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();


        Criteria criteria = session.createCriteria(Customer.class);

        List list = criteria.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 条件查询

    @Test
    public void testFunc02() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();


        Criteria criteria = session.createCriteria(Customer.class);

        // 体贴写法
        criteria.add(Restrictions.idEq(1l));

        // 通用条件
        criteria.add(Restrictions.eq("cust_id", 1l));

        Customer customer = (Customer) criteria.uniqueResult();

        System.out.println(customer);

        transaction.commit();

        session.close();
    }
  1. 分页查询 - 和HQL一样
@Test
    public void testFunc03() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();


        Criteria criteria = session.createCriteria(Customer.class);

        criteria.setFirstResult(1);

        criteria.setMaxResults(1);

        List list = criteria.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 排序
@Test
    public void testFunc04() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();


        Criteria criteria = session.createCriteria(Customer.class);

        // 升序
        criteria.addOrder(Order.asc("cust_od"));

        // 降序
        criteria.addOrder(Order.desc("cust_od"));

        
        List list = criteria.list();

        System.out.println(list);

        transaction.commit();

        session.close();
    }
  1. 聚合函数查询
@Test
    public void testFunc05() {

        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        // 行数
        Criteria criteria = session.createCriteria(Customer.class);

        criteria.setProjection(Projections.rowCount());

        System.out.println(criteria.list());

        transaction.commit();

        session.close();
    }

3. 离线查询对象

// 离线查询
    @Test
    public void testFunc06() {

        // web层
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);


        // service层
        Session session = HibernateUtils.openSession();

        Transaction transaction = session.beginTransaction();

        
        // dao层
        Criteria criteria = detachedCriteria.getExecutableCriteria(session);

        List list = criteria.list();
                
        System.out.println(list);

        transaction.commit();

        session.close();
    }

4. 查询优化-策略

4.1 类级别加载策略

@Test
    // get方法 : 立即加载.执行方法时立即发送sql语句查询结果
    public void testFunc01(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
                // 在这句执行sql查询操作
        Customer c = session.get(Customer.class, 1l);
        
        System.out.println(c);
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
@Test
    // load方法(默认):是在执行时,不发送任何sql语句.返回一个对象.使用该对象时,才执行查询.
    // 延迟加载: 仅仅获得没有使用.不会查询.在使用时才进行查询.
    // 是否进行延迟加载: 可以通过在class元素上配置lazy属性来控制.
        //lazy:true  加载时,不查询.使用时才查询,使用代理对象Customer$$来完完成数据库查询
        //lazy:false 加载时立即查询.
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Customer c = session.load(Customer.class, 1l);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        System.out.println(c);
        
    }

// 配置文件


        
        
        
            
        

4.2 关联级别查询策略

  1. 集合策略
    也就是根据Customer来获取LinkMan

配置文件Customer.hbm.xml


            
                
                
            
//关联级别 延迟加载 & 抓取策略
public class Demo {
    
    //集合级别的关联
    //fetch:select 单表查询
    //lazy:true 使用时才加载集合数据.
    @Test
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Customer c = session.get(Customer.class, 2l);
        
        Set linkMens = c.getLinkMens();//关联级别
        
        System.out.println(linkMens);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    //集合级别的关联
        //fetch:select 单表查询
        //lazy:false 立即加载集合数据
        @Test
        public void fun2(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
            
            Customer c = session.get(Customer.class, 2l);
            
            Set linkMens = c.getLinkMens();//关联级别
            
            System.out.println(linkMens);
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
        //集合级别的关联
        //fetch:select 单表查询
        //lazy:extra 极其懒惰.与懒加载效果基本一致. 如果只获得集合的size.只查询集合的size(count语句)
        @Test
        public void fun3(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
            
            Customer c = session.get(Customer.class, 2l);
            
            Set linkMens = c.getLinkMens();//关联级别
            
            System.out.println(linkMens.size());
            
            System.out.println(linkMens);
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
        //集合级别的关联
        //fetch:join    多表查询
        //lazy:true|false|extra 失效.立即加载.
        @Test
        public void fun4(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
            
            Customer c = session.get(Customer.class, 2l);
            
            Set linkMens = c.getLinkMens();//关联级别
            
            System.out.println(linkMens.size());
            
            System.out.println(linkMens);
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
        
        @Test
        //fetch: subselect 子查询
        //lazy: true 懒加载
        public void fun5(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
                
            String  hql = "from Customer";
            
            Query query = session.createQuery(hql);
            
            List list = query.list();
            
            for(Customer c:list){
                System.out.println(c);
                System.out.println(c.getLinkMens().size());
                System.out.println(c.getLinkMens());
            }
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
        @Test
        //fetch: subselect 子查询
        //lazy: false 立即加载
        public void fun6(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
                
            String  hql = "from Customer";
            
            Query query = session.createQuery(hql);
            
            List list = query.list();
            
            for(Customer c:list){
                System.out.println(c);
                System.out.println(c.getLinkMens().size());
                System.out.println(c.getLinkMens());
            }
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
        @Test
        //fetch: subselect 子查询
        //lazy: extra 极其懒惰
        public void fun7(){
            Session session = HibernateUtils.openSession();
            Transaction tx = session.beginTransaction();
            //----------------------------------------------------
                
            String  hql = "from Customer";
            
            Query query = session.createQuery(hql);
            
            List list = query.list();
            
            for(Customer c:list){
                System.out.println(c);
                System.out.println(c.getLinkMens().size());
                System.out.println(c.getLinkMens());
            }
            
            //----------------------------------------------------
            tx.commit();
            session.close();
            
        }
    
}
  1. 对象策略
    配置文件 LinkMan.hbm.xml

        
        
    

根据LinkMan获得Customer

//关联级别 延迟加载 & 抓取策略
public class Demo2 {
    
    @Test
    //fetch:select  单表查询
    //lazy:proxy  
        //customer-true 懒加载
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        LinkMan lm = session.get(LinkMan.class, 3l);
        
        Customer customer = lm.getCustomer();
        
        System.out.println(customer);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    @Test
    //fetch:join    多表
    //lazy: 失效  
    public void fun3(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        LinkMan lm = session.get(LinkMan.class, 3l);
        
        Customer customer = lm.getCustomer();
        
        System.out.println(customer);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    @Test
    //fetch:select  单表查询
    //lazy:proxy  
        //customer-false 立即加载
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        LinkMan lm = session.get(LinkMan.class, 3l);
        
        Customer customer = lm.getCustomer();
        
        System.out.println(customer);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
}

结论:全部使用默认值

  1. 批量抓取 - 提高效率
    配置文件 Customer.hbm.xml

        
            
            
        

每次抓取三个LinkMan,减少sql操作

//抓取数量
public class Demo {
    
    @Test
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        String hql = "from Customer ";
        Query query = session.createQuery(hql);
        List list = query.list();
        
        for(Customer c:list){
            System.out.println(c.getLinkMens());
        }
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }

5. 使用离线查询DetachedCriteria,完成客户列表筛选功能

  1. CustomerListServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String cust_name = request.getParameter("cust_name");

        List list = null;

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);

        if (cust_name != null && !"".equals(cust_name)) {

            detachedCriteria.add(Restrictions.like("cust_name", "%" + cust_name + "%"));

            list = customerService.getCustomerByDetachedCriteria(detachedCriteria);

            System.out.println("-----条件查询-----" + cust_name);

        } else  {

            list = customerService.getAll(detachedCriteria);

            System.out.println("------全部查询-------");
        }


        // 将客户列表放到request域
        request.setAttribute("list", list);

        // 转发到list.jsp
        request.getRequestDispatcher("/jsp/customer/list.jsp").forward(request, response);
    }
  1. CustomerService 接口和 CustomerServiceImpl 实现类
    public List getAll(DetachedCriteria detachedCriteria);

    public List getCustomerByDetachedCriteria(DetachedCriteria detachedCriteria);

这里可以进行方法的抽取,只在servlet中进行离线DetachedCriteria 的条件组装即可,service和dao中不需要再修改方法,一个就够了

 @Override
    public List getAll(DetachedCriteria detachedCriteria) {

        Session session = HibernateUtils.getCurrentSession();

        // 打开事务
        Transaction transaction = session.beginTransaction();

        List list = customerDao.getAll(detachedCriteria);

        transaction.commit();

        return list;
    }

    @Override
    public List getCustomerByDetachedCriteria(DetachedCriteria detachedCriteria) {

        Session session = HibernateUtils.getCurrentSession();

        Transaction transaction = session.beginTransaction();

        List list = customerDao.getCustomerByDetachedCriteria(detachedCriteria);

        transaction.commit();

        return list;
    }
  1. CustomerDao 接口和 CustomerDaoImpl 实现类
public List getAll(DetachedCriteria detachedCriteria);

    public List getCustomerByDetachedCriteria(DetachedCriteria detachedCriteria);
@Override
    public List getAll(DetachedCriteria detachedCriteria) {

        Session session = HibernateUtils.getCurrentSession();

        Criteria criteria = detachedCriteria.getExecutableCriteria(session);

        return criteria.list();
    }


    @Override
    public List getCustomerByDetachedCriteria(DetachedCriteria detachedCriteria) {

        Session session = HibernateUtils.getCurrentSession();

        Criteria criteria = detachedCriteria.getExecutableCriteria(session);

        return criteria.list();
    }

你可能感兴趣的:(Java mac idea hibernate的使用04)