Session session = new
Configuration().configure().buildSessionFactory().openSession();
à简单实例说明:
àEqual:
public class HibernateDemo01 {
/**
* @param args
*/
public
static void main(String[] args) {
Session
session = new Configuration().configure().buildSessionFactory()
.openSession();
Criteria cri = session.createCriteria(Person.class);
cri.add(Expression.eq("name", "张三"));
List
all = cri.list();
if
(all.size() > 0) {
Person
per = (Person) all.get(0);
System.out.println(per.getName()
+ " --> " + per.getSex());
}
}
}
à大于
Criteria cri =
session.createCriteria(Person.class);
cri.add(Expression.gt("age",
25));
List all =
cri.list();
Iterator iter =
all.iterator();
while
(iter.hasNext()) {
Person per =
(Person) iter.next() ;
System.out.println(per.getName() + "
--> " + per.getSex() + " --> "+ per.getAge());
}
à小于
Criteria cri = session.createCriteria(Person.class);
cri.add(Expression.lt("age", 25));
àbetween:
Criteria cri =
session.createCriteria(Person.class);
cri.add(Expression.between("age",
25, 35));
à模糊查询:
Criteria cri = session.createCriteria(Person.class);
cri.add(Expression.like("name","%三%"));
àin
Criteria cri = session.createCriteria(Person.class);
Set<Integer>
conds = new TreeSet<Integer>();
conds.add(new
Integer(1));
conds.add(new
Integer(2));
cri.add(Expression.in("id",
conds));
àand
Criteria cri =
session.createCriteria(Person.class);
cri.add(Expression.and(Expression.eq("name", "张三"),
Expression.eq("sex", "男")));
àExample类查询条件跟已知对象的条件相同
Person person = new Person() ;
person.setName("张三") ;
person.setSex("男") ;
person.setAge(20)
;
cri.add(Example.create(person));
-- 学海无涯