HQL語句--where(限制)

where子句:邏輯表達式。用來設置查詢的條件來限制返回的查詢結果。


比較運算:將持久化類的屬性與給定的查詢條件來進行比較。
比較運算符:

  1. =、<>、<、>、>=、<=
  2. null值判斷— is [not] null 或者使用相等(=)、不等(<>)進行判斷。x=null -> x is null; x<>null -> x is not null
@Test
    public void testWhere1(){
        String hql="from Commodity c where c.price>400";
        Query query=session.createQuery(hql);       
        List<Commodity> commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName());
            System.out.println("price:"+commodity.getPrice());
        }
    }

控制檯:
這裏寫圖片描述

@Test
    public void testWhere1(){
        String hql="from Commodity c where c.description is null";//也可寫爲c.description = null
        Query query=session.createQuery(hql);       
        List<Commodity> commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName());
            System.out.println("description:"+commodity.getDescription());
        }
    }

控制檯:
這裏寫圖片描述


範圍運算:判斷屬性值是否在給定的條件範圍之內

  1. [not] in (列表),屬性值在列表中存在,返回true;否則返回false。(not取反)
  2. [not] between 值1 and 值2,屬性值在範圍間返回true,否則返回false。
@Test
    public void testWhere2(){
        String hql="from Customer c where c.age in(20,40)"; //查詢顧客年齡爲20或40
        Query query=session.createQuery(hql);       
        List<Customer> customers=query.list();

        for (Customer customer : customers) {
            System.out.println("name:"+customer.getName());
            System.out.println("age:"+customer.getAge());
        }
    }

這裏寫圖片描述

@Test
    public void testWhere2(){
        String hql="from Customer c where c.age between 20 and 40"; //查詢顧客年齡在20到40間
        Query query=session.createQuery(hql);       
        List<Customer> customers=query.list();

        for (Customer customer : customers) {
            System.out.println("name:"+customer.getName());
            System.out.println("age:"+customer.getAge());
        }
    }

這裏寫圖片描述


字符串模式匹配

  1. like 關鍵字,對字符串類型屬性進行匹配運算
  2. 通配符 % (匹配任意個字符) 、 _ (匹配一個字符)
@Test
    public void testWhere3(){
        String hql="from Customer c where c.name like '張_'"; //查詢姓張且名字爲兩個字的顧客
        Query query=session.createQuery(hql);
        List<Customer> customers=query.list();

        for (Customer customer : customers) {
            System.out.println("name:"+customer.getName());
        }
    }

這裏寫圖片描述

@Test
    public void testWhere3(){
        String hql="from Customer c where c.address like '%北京%'"; //查詢地址在北京的顧客
        Query query=session.createQuery(hql);
        List<Customer> customers=query.list();

        for (Customer customer : customers) {
            System.out.println("name:"+customer.getName());
            System.out.println("address:"+customer.getAddress());
        }
    }

這裏寫圖片描述


邏輯運算:通過邏輯運算符將一個邏輯表達式按照邏輯運算規則形成一個新的邏輯表達式。
運算符:

  1. and(邏輯與)、or(邏輯或)
  2. not(邏輯非):取反
@Test
    public void testWhere2(){
        //查詢商品價格在100到500之間並且它的類型是電腦
        String hql="from Commodity c where c.price between 100 and 5000 and c.category like '%電腦%'";
        Query query=session.createQuery(hql);
        List<Commodity> commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName()); //商品名稱
            System.out.println("category:"+commodity.getCategory()); //商品分類
            System.out.println("price:"+commodity.getPrice()); //商品價格
        }
    }

這裏寫圖片描述


集合運算:在持久化類映射中,存在一對多的屬性映射配置,可以通過集合運算符來做相應的判定運算。

  1. is [not] empty 集合[不]爲空,不包含任何元素
  2. member of 判斷元素(實例)是否屬於集合

empty->exists ; member of -> in

@Test
    public void testWhere1(){
        String hql="from Order o where o.orderItems is not empty"; //查詢訂單明細不爲空的訂單
        Query query=session.createQuery(hql);
        List<Order> orders=query.list();

        for (Order order : orders) {
            System.out.println(order.getCustomer().getName()); //訂單的顧客信息
            System.out.println(order.getAmount()); //訂單總金額
            System.out.println(order.getTradeDate()); //訂單交易日期
        }
    }

控制檯,(部分):
這裏寫圖片描述


四則運算:可以在where子句和select子句使用

  1. +
  2. -
  3. *
  4. /
@Test
    public void testWhere4(){
        String hql="from Commodity c where c.price*5>3000" ;//查詢五件商品價格大於3000元的商品
        Query query=session.createQuery(hql);
        List<Commodity> commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName());
            System.out.println("price:"+commodity.getPrice()*5);
        }
    }

這裏寫圖片描述


查詢單個對象:Query接口的uniqueResult方法,其查詢返回結果只存在一個實例對象(或者不存在),而不是List集合。若結果返回多個對象,該方法會拋出異常。該方法要通過where子句條件的設置。

@Test
    public void testWhere4(){
        String hql="from Customer c where c.name='張三'" ; //查詢姓名爲張三的顧客
        Query query=session.createQuery(hql);

        Customer c=(Customer) query.uniqueResult(); //返回一個實例對象
        System.out.println(c.getName());
    }

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章