Cyh的博客

Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220

Criteria查询表达式

Posted on 2009-02-16 19:41 啥都写点 阅读(364) 评论(0)  编辑  收藏 所属分类: J2EE

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));



                                                                                                       --    学海无涯
        


只有注册用户登录后才能发表评论。


网站导航: