getHibernateTemplate()(Spring中常用的hql查询方法)

Spring中常用的hql查询方法getHibernateTemplate()

 

---------------------------------
一、find(String queryString);
      
示例:this.getHibernateTemplate().find("from bean.User");
      
返回所有User对象
 
二、find(String queryString , Object value);
      
示例:this.getHibernateTemplate().find("from bean.User u where u.name=?", "test");

      或模糊查询:this.getHibernateTemplate().find("from bean.User u where u.name like ?", "%test%");
      
返回name属性值为test的对象(模糊查询,返回name属性值包含test的对象)
 
三、find(String queryString, Object[] values);
      
示例:String hql= "from bean.User u where u.name=? and u.password=?"
               
 this.getHibernateTemplate().find(hql, new String[]{"test", "123"});
      
返回用户名为test并且密码为123的所有User对象
 
---------------------------------
四、findByExample(Object exampleEntity)
      
示例:
             
User u=new User();   
           
  u.setPassword("123");//必须符合的条件但是这两个条件时并列的(象当于sql中的and   
           
  u.setName("bb");   
             list=this.getHibernateTemplate().findByExample(u,start,max); 
      返回:用户名为bb密码为123的对象
 
五、findByExample(Object exampleEntity, int firstResult, int maxResults)
      
示例:
           
 User u=new User();   
           
 u.setPassword("123");//必须符合的条件但是这两个条件时并列的(象当于sql中的and   
            
u.setName("bb");   
            list=this.getHibernateTemplate().findByExample(u,start,max);   
      
返回:满足用户名为bb密码为123,自start起共maxUser对象。(对象从0开始计数)
 
---------------------------------------------------
六、findByNamedParam(String queryString , String paramName , Object value)
 
    
使用以下语句查询:
     
    String queryString = "select count(*) from bean.User u where u.name=:myName";
         String paramName= "myName";
         String value= "xiyue";
         this.getHibernateTemplate().findByNamedParam(queryString, paramName, value);
         System.out.println(list.get(0));

     
返回namexiyueUser对象的条数
 
七、findByNamedParam(String queryString , String[] paramName , Object[] value)
      示例:
         
String queryString = "select count(*) from bean.User u where u.name=:myName and u.password=:myPassword";
         String[] paramName= new String[]{"myName", "myPassword"};
         String[] value= new String[]{"xiyue", "123"};
         this.getHibernateTemplate().findByNamedParam(queryString, paramName, value);
         返回用户名为xiyue密码为123User对象
 
八、findByNamedQuery(String queryName)
      
示例:
        1
、首先需要在User.hbm.xml中定义命名查询
             <hibernate-mapping>
                  <class>......</class>
                  <query name="queryAllUser">
                       <![CDATA[
                            from bean.User
                        ]]>
                  </query>
             </hibernate-mapping>

         2
、如下使用查询:
        
     this.getHibernateTemplate().findByNamedQuery("queryAllUser");
 
九、findByNamedQuery(String queryName, Object value)
      
示例:
        1
、首先需要在User.hbm.xml中定义命名查询
             
<hibernate-mapping>
                  <class>......</class>
                  <query name="queryByName">
                       <![CDATA[
                            from bean.User u where u.name = ?
                        ]]>
                  </query>
             </hibernate-mapping>
         2、如下使用查询:
     this.getHibernateTemplate().findByNamedQuery("queryByName", "test");
 
十、findByNamedQuery(String queryName, Object[] value)
      
示例:
        1
、首先需要在User.hbm.xml中定义命名查询
             
<hibernate-mapping>
                  <class>......</class>
                  <query name="queryByNameAndPassword">
                       <![CDATA[
                from bean.User u where u.name =? and u.password =?
                        ]]>
                  </query>
             </hibernate-mapping>

         2
、如下使用查询:
             
String[] values= new String[]{"test", "123"};
      this.getHibernateTemplate().findByNamedQuery("queryByNameAndPassword" , values);

 
十一、findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
 示例:
        1
、首先需要在User.hbm.xml中定义命名查询
             <hibernate-mapping>
                  <class>......</class>
                  <query name="queryByName">
                       <![CDATA[
                            from bean.User u where u.name =:myName
                        ]]>
                  </query>
             </hibernate-mapping>

         2
、如下使用查询:
             
this.getHibernateTemplate().findByNamedQuery("queryByName" , "myName", "test");
 
十二、findByNamedQueryAndNamedParam(String queryName, String[] paramName, Object[] value)
 
 
             
this.getHibernateTemplate().findByNamedQuery("queryByNameAndPassword" , names, values);
 
 
十三、findByValueBean(String queryString , Object value);
 
示例:
      1
、定义一个ValueBean,属性名必须和HSQL语句中的:后面的变量名同名,此处必须至少有两个属性,分别为myNamemyPassword,使用setter方法设置属性值后
       
   ValueBean valueBean= new ValueBean();
      valueBean.setMyName("test");
      valueBean.setMyPasswrod("123");

      2

          
String queryString= "from bean.User u where u.name=:myName and u.password=:myPassword";
     this.getHibernateTemplate().findByValueBean(queryString , valueBean);
       
 
十四、findByNamedQueryAndValueBean(String queryName , Object value);
 
示例:
       1
、首先需要在User.hbm.xml中定义命名查询
          
   <hibernate-mapping>
                  <class>......</class>
                  <query name="queryByNameAndPassword">
                       <![CDATA[
       from bean.User u where u.name =:myName and u.password=:myPassword
                        ]]>
                  </query>
             </hibernate-mapping>

      2
、定义一个ValueBean,属性名必须和User.hbm.xml命名查询语句中的:后面的变量名同名,此处必须至少有两个属性,分别为myNamemyPassword,使用setter方法设置属性值后
         
 ValueBean valueBean= new ValueBean();
     valueBean.setMyName("test");
     valueBean.setMyPasswrod("123");

 
 
 
      3

          
String queryString= "from bean.User u where u.name=:myName and u.password=:myPassword";
     this.getHibernateTemplate().findByNamedQueryAndValueBean("queryByNameAndPassword", valueBean);

 
示例:
        1
、首先需要在User.hbm.xml中定义命名查询
             <hibernate-mapping>
                  <class>......</class>
                  <query name="queryByNameAndPassword">
                       <![CDATA[
       from bean.User u where u.name =:myName and u.password=:myPassword
                        ]]>
                  </query>
             </hibernate-mapping>

         2
、如下使用查询:
     String[] names= new String[]{"myName", "myPassword"};
     String[] values= new String[]{"test", "123"};

posted @ 2014-05-02 11:29  baoendemao  阅读(470)  评论(0编辑  收藏  举报