where子句:逻辑表达式。用来设置查询的条件来限制返回的查询结果。
比较运算:将持久化类的属性与给定的查询条件来进行比较。
比较运算符:
- =、<>、<、>、>=、<=
- 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());
}
}
控制台:
范围运算:判断属性值是否在给定的条件范围之内
- [not] in (列表),属性值在列表中存在,返回true;否则返回false。(not取反)
- [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());
}
}
字符串模式匹配:
- like 关键字,对字符串类型属性进行匹配运算
- 通配符 % (匹配任意个字符) 、 _ (匹配一个字符)
@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());
}
}
逻辑运算:通过逻辑运算符将一个逻辑表达式按照逻辑运算规则形成一个新的逻辑表达式。
运算符:
- and(逻辑与)、or(逻辑或)
- 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()); //商品价格
}
}
集合运算:在持久化类映射中,存在一对多的属性映射配置,可以通过集合运算符来做相应的判定运算。
- is [not] empty 集合[不]为空,不包含任何元素
- 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子句使用
+
加-
减*
乘/
除
@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());
}