使用QBE查询,我们可以将查询数据封装成对象,传递给Example进行查询,美中不足的是,QBE方式不能只能支持对象属性字段的等于查询,和字符串的模糊匹配,不能支持大于,等于的操作 package Search.QBE; import java.io.File; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Example; import org.hibernate.criterion.MatchMode; public class Test ... { public static void main(String[] args)...{ String filePath=System.getProperty("user.dir")+File.separator+"src/Search/QBE"+File.separator+"hibernate.cfg.xml"; File file=new File(filePath); SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory(); Session session=sessionFactory.openSession(); Transaction t=(Transaction)session.beginTransaction(); TestStu stu=new TestStu(); stu.setAge(15); stu.setName("tom5"); Example exampleStu=Example.create(stu). //查询条件为age=15 and name like %tom5%,并忽略大小写 ignoreCase().enableLike(MatchMode.ANYWHERE); //忽略age字段 //ignoreCase().enableLike(MatchMode.ANYWHERE).excludeProperty("age"); List stuList=session.createCriteria(TestStu.class).add(exampleStu).list(); for (Iterator iterator = stuList.iterator(); iterator.hasNext();) ...{ TestStu object = (TestStu) iterator.next(); System.out.println(object.getName()); } t.commit(); }}