Hibernate QBC的查询方式的总结

Hibernate QBC的查询方式的总结

(zhuan http://hi.baidu.com/liuchaoping66/blog/item/16a8858a575636d4fd1f10c7.html)
1          Criteria criteria = this.getSession().createCriteria(EquipmentPO.class);
2          criteria. add (Restrictions.allEq(new Map(Restrictions.eq("equipID",new String(" 11020449 ")),Restrictions.eq("equipID",new String(" 11020449 "))));        
3          // 注意:between查询条件可解释为查询EquipmentPO对象中的equipID属性值在new String(" 11020449 ")和new String(" 11030137 ")之间的所有记录值(包含两个端点)
4          criteria. add (Restrictions. between ("equipID",new String(" 11020449 "),new String(" 11030137 ")));
5          // 查询结果列表记录按照equipID的属性值来升序排序
6          criteria.addOrder( Order . asc ("equipID")); // 降序方式为: Order . desc ("equipID")        
7          // 通过EquipmentPO对象的主键id来查询
8          criteria. add (Restrictions.idEq("402882ac0d3f7ca8010d3f7ef869000b"));        
9          Restrictions. like (fieldName, " % " + para + " % ")
10          // 使用ilike方式进行模糊查询
11          criteria. add (Restrictions.ilike("equipID",new String(" % 11020 % ")));
12          // ilike的i即ignore之意,所以这里查询出englishName值为"Optima XL 100K Ultracentrifuge"(忽略大小写)的记录
13          criteria. add (Restrictions.ilike("englishName",new String("Optima XL 100K Ultracentrifuge"),MatchMode.ANYWHERE)); // 这里        
14          // 使用in方式有两种形式,即数组或者Collection的方式,可参考如下两个实例
15          // criteria. add (Restrictions. in ("equipID",new String [] {" 11020449 "," 11020450 "})); // 数组参数
16          Collection col = new ArrayList();
17          col. add (new String(" 11020449 "));
18          col. add (new String(" 11020450 "));
19          col. add (new String(" 11020874 "));
20          criteria. add (Restrictions. in ("equipID",col)); // Collection参数        
21          // 使用isEmpty / isNotEmpty方式用来判断EquipmentPO对象中的Collection类型的属性是否为空的所有记录
22          // EquipmentPO对象中定义属性private Set equipFunctionDevelopPOs = new HashSet(); // 设备功能开发对象
23          criteria. add (Restrictions.isEmpty("equipFunctionDevelopPOs"));
24          criteria. add (Restrictions.isNotEmpty("equipFunctionDevelopPOs"));
25          // 使用isNull方式查询出所有schoolID属性没有值的记录。说明:Restrictions.isNull判断属性是否为空,为空返回true,反之返回false
26          criteria. add (Restrictions. isNull ("schoolID"));
27          criteria. add (Restrictions.isNotNull("schoolID"));        
28          // 与Restrictions.eq正好相反,表示不存在( not in )
29          criteria. add (Restrictions. not (Restrictions.eq("equipID",new String(" 11020449 "))));
30          // 使用Restrictions.sizeEq方式用来查询EquipmentPO对象中的Collection类型的属性equipFunctionDevelopPOs的size为1的所有记录
31          criteria. add (Restrictions.sizeEq("equipFunctionDevelopPOs", 1 ));        
32          // 使用sql限定的查询。注意{alias}.chnname这里是指实际表中的字段名而非属性名(不区分大小写)
33          criteria. add (Restrictions.sqlRestriction("{alias}.chnname like (?)"," % % ",Hibernate.STRING));
34          criteria. add (Restrictions.sqlRestriction("{alias}.ENGNAME like (?)"," % ptima % ",Hibernate.STRING));
35          // 如果有多个查询条件,比如between子句的查询则如下:        
36          BigDecimal [] unitPrice = {new BigDecimal( 402514 ),new BigDecimal( 614891 )};
37          Type [] types = {Hibernate.BIG_DECIMAL,Hibernate.BIG_DECIMAL};
38          criteria. add (Restrictions.sqlRestriction("{alias}.unit_price between (?) and (?)",unitPrice,types));
39          List list = criteria.list();
40          System.out.println("size ===> " + list.size());
41          return list;

你可能感兴趣的:(Hibernate QBC的查询方式的总结)