hql语句命名参数查询示例

Session  session = this.getSession();

 

String hql = "from TblFwxx fw where fw.title like ?";

 

Query query = session.createQuery(hql);

 

query.setString(0,"%艳儿%");

 

上面使用“?”作占位符,然后通过Query的setString方法逐个设置参数的值。但这样降低了代码的可读性。

所以以后尽量使用“命名参数查询”来解决这些问题

 

把上面的hql语句改为:  String hql = "from TblFwxx fw where fw.tltle like :zt";

                                 

                               Query query = session.createQuery(hql);

 

                               query.setString("zt","艳儿");

 

 

 

=============================

以前还讲到了Criteria查询  (也是对象查询)  

 

   主要是Criteria对象封装查询条件,并提供Restrictions等类别作辅助。  (具体用法请查找网络资源)

例:    public List search(TblFwxx condition){

                 Session session = this.getSession();

                 Criteria c = session.createCriteria(TblFwxx.class);

                  if(null != condition){

                        if(condition.getTitle() != null && !condition.getTitle().equals(" ")){

                                c.add(Restrictions.like("title",condition.getTitle(),MatchMode.ANYWHERE));

                        }

                  }    

         }

 

你可能感兴趣的:(hibernate)