创建一个Criteria实例 net.sf.hibernate.Criteria这个接口代表对一个特定的持久化类的查询。Session是用来制造Criteria实例的工厂。
package com.haha.test3;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import com.haha.dao.HibernateSessionFactory;
import com.haha.info.Books;
public class Test或者查询 {
public static void main(String[] args) {
//查询 java 或 oracle 或 jsp的图书
Criteria crit=HibernateSessionFactory.getSession()
.createCriteria(Books.class);
Criterion where1=Restrictions.ilike("title", "java",MatchMode.ANYWHERE);
Criterion where2=Restrictions.ilike("title", "oracle",MatchMode.ANYWHERE);
Criterion where3=Restrictions.ilike("title", "jsp",MatchMode.ANYWHERE);
Criterion where4=Restrictions.or(where1, where2);
crit.add(Restrictions.or(where3, where4));
List<Books> list=crit.list();
for(Books bk:list){
System.out.println(bk.getTitle()+","+bk.getUnitprice());
}
System.out.println("共有"+list.size()+"条");
}
}
第二种方法