资源描述:
《gethibernatetemplate.find方法详解》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、getHibernateTemplate().find方法详解Spring中常用的hql查询方法(getHibernateTemplate())---------------------------------一、find(StringqueryString); 示例:this.getHibernateTemplate().find("frombean.User"); 返回所有User对象二、find(StringqueryString,Objectvalue); 示例:this.getHibernateTemplate().f
2、ind("frombean.Useruwhereu.name=?","test"); 或模糊查询:this.getHibernateTemplate().find("frombean.Useruwhereu.namelike?","%test%"); 返回name属性值为test的对象(模糊查询,返回name属性值包含test的对象)三、find(StringqueryString,Object[]values); 示例:Stringhql="frombean.Useruwhereu.name=?andu.password=?"
3、 this.getHibernateTemplate().find(hql,newString[]{"test","123"}); 返回用户名为test并且密码为123的所有User对象---------------------------------四、findByExample(ObjectexampleEntity) 示例: Useru=newUser(); u.setPassword("123");//必须符合的条件但是这两个条件时并列的(象当于sql中的
4、and) u.setName("bb"); list=this.getHibernateTemplate().findByExample(u,start,max); 返回:用户名为bb密码为123的对象五、findByExample(ObjectexampleEntity,intfirstResult,intmaxResults) 示例: Useru=newUser(); u.setPassword("123");//必须符合的条件但是这两
5、个条件时并列的(象当于sql中的and) u.setName("bb"); list=this.getHibernateTemplate().findByExample(u,start,max); 返回:满足用户名为bb密码为123,自start起共max个User对象。(对象从0开始计数)---------------------------------------------------六、findByNamedParam(StringqueryString,StringparamN
6、ame,Objectvalue) 使用以下语句查询: StringqueryString="selectcount(*)frombean.Useruwhereu.name=:myName"; StringparamName="myName"; Stringvalue="http://www.cnblogs.com/gywbg/archive/2012/04/28/xiyue"; this.getHibernateTemplate().findByNamedParam(queryString,par
7、amName,value); System.out.println(list.get(0)); 返回name为xiyue的User对象的条数七、findByNamedParam(StringqueryString,String[]paramName,Object[]value) 示例: StringqueryString="selectcount(*)frombean.Useruwhereu.name=:myNameandu.password=:myPassword"; String[]paramNa
8、me=newString[]{"myName","myPassword"}; String[]value=http://ww