【转】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起共max个User对象。(对象从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));

     返回name为xiyue的User对象的条数

 

七、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密码为123的User对象

 

八、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)


 示例:

        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"};

 

             this.getHibernateTemplate().findByNamedQuery("queryByNameAndPassword" , names, values);

 

 十三、findByValueBean(String queryString , Object value);

 示例:

      1、定义一个ValueBean,属性名必须和HSQL语句中的:后面的变量名同名,此处必须至少有两个属性,分别为myName和

myPassword,使用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命名查询语句中的:后面的变量名同名,此处必须至少有两个属性,分别

为myName和myPassword,使用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);

 

来源:http://blog.sina.com.cn/s/blog_643014ef0100h28r.html

posted @ 2016-06-25 11:06  run127  Views(273)  Comments(0Edit  收藏  举报