findByExample和findByCriteria的用法

来源:互联网 发布:淘宝店铺设计效果图 编辑:程序博客网 时间:2024/06/05 01:34

通过今天的检索可以得出这样的一个结论就是如果是精确查询那么一般用findbyexample

相反如是模糊查询一般用findByCriteria方法。

//return this.getHibernateTemplate().findByExample(example).size();

------------------------------------------------findByCriteria------------------------------
   DetachedCriteria detachedCrit = DetachedCriteria.forClass(ZtResource.class);
   Example example = Example.create(zt_resource).ignoreCase().enableLike(MatchMode.ANYWHERE);
   detachedCrit.add(example);  
return getHibernateTemplate().findByCriteria(detachedCrit).size();

---------------------------------------------------------result------------------------------

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   ZtResourceDao resourcedao = (ZtResourceDao) context.getBean("ZtResourceDao");
   ZtResource resource = new ZtResource();
   resource.setLunwenTiming("格式识别及版面自动分析");
   System.out.println("size===="+resourcedao.getListByObjectRows(resource));

--------------------------------------------------------------------------------------------

findByCriteria.size=1

findByExample.size=0(必须是“PS格式识别及版面自动分析”)才行------结论精确查找

 

 


 

findByExample()使用时注意
1.不支持主键 2.不支持关联 3.不支持NULL 作为查询条件,findByExample会忽略参数中关联的实体类属性中的值
原创粉丝点击