//查找全部
Criteria cr= session.createCriteria(Users.class);
//添加各种条件
//1 LIKE查询
Criterion c1= Expression.like("name","t%");
//2 等于 age=12
Criterion c2 = Expression.eq("age", 12);
//3 大于 age>12
Criterion c3 = Expression.gt("age", 12);
//4 不等于 age!=12
Criterion c4 = Expression.not(Expression.eq("age", 12));
//5 age为空
Criterion c5 = Expression.isNull("age");
//6 in语句
Criterion c6 = Expression.in("name", new String[]{"tome","jack","jerry"});
//7 between 语句
Criterion c7 = Expression.between("age", 12, 15);
//8 以t开头,长度为3的名字,并且忽略大小写
Criterion c8 = Expression.like("name", "t_ _").ignoreCase();
//添加查询条件并按age降序排列
cr.add(c1).addOrder(Order.desc("age"));
cr.add(c2);
方法二:将查询语句存入相应的配置文件中,这样可以在程序中调用
<![CDATA[
from Users
]]>
</query>
<!-- 带参数的 -->
<query name="lookName">
<![CDATA[
from Users p where p.name =:name
]]>
</query>
在程序中这样调用
Query query=session.getNamedQuery("lookName");
query.setString("name","tom");
List list=query.list();
//List list=cr.list();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Users element = (Users) iterator.next();
System.out.println(element.getName());
}