Hql Query method commonly used in "Go" Spring (gethibernatetemplate ())

Source: Internet
Author: User
Tags cdata

One, find (String queryString);

Example: This.gethibernatetemplate (). Find ("From Bean". User ");

Returns all user objects

Two, find (String queryString, Object value);

Example: This.gethibernatetemplate (). Find ("From Bean". User u where u.name=? "," Test ");

or fuzzy query: This.gethibernatetemplate (). Find ("From Bean". User u where u.name like? ","%test% ");

Returns an object with the Name property value of test (fuzzy query that returns the object with the Name property value containing test)

Third, find (String queryString, object[] values);

Example: String hql= "from Bean. User u where u.name=? and u.password=? "

This.gethibernatetemplate (). Find (HQL, new string[]{"test", "123"});

Returns all user objects with a username of test and a password of 123

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

Iv. findbyexample (Object exampleentity)

Example:

User U=new user ();

U.setpassword ("123");//must meet the conditions but the two conditions are parallel (as in SQL)

U.setname ("BB");

List=this.gethibernatetemplate (). Findbyexample (U,start,max);

Return: The user name is a BB password of 123 object

V. Findbyexample (Object exampleentity, int firstresult, int maxResults)

Example:

User U=new user ();

U.setpassword ("123");//must meet the conditions but the two conditions are parallel (as in SQL)

U.setname ("BB");

List=this.gethibernatetemplate (). Findbyexample (U,start,max);

Returns: satisfies the user name of the BB password is 123, starting from start a total of Max user objects. (object counting starting from 0)

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

VI, Findbynamedparam (string queryString, String paramname, Object value)

Use the following statement to query:

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));

Returns the number of bars for the user object named Xiyue

Vii. Findbynamedparam (String queryString, string[] paramname, object[] value)

Example:

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);

Returns the user object named Xiyue with a password of 123

Viii. findbynamedquery (String queryname)

Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Queryalluser" ><!--This query is called by name--

<! [cdata[

From Bean. User

]]>

</query>

2. Use the following query:

This.gethibernatetemplate (). Findbynamedquery ("Queryalluser");

IX, Findbynamedquery (String queryname, Object value)

Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Querybyname" ><!--This query is called by name--

<! [cdata[

From Bean. User u where u.name =?

]]>

</query>

2. Use the following query:

This.gethibernatetemplate (). Findbynamedquery ("Querybyname", "Test");

X. Findbynamedquery (String queryname, object[] value)

Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Querybynameandpassword" ><!--This query is called by name--

<! [cdata[

From Bean. User u where u.name =? and U.password =?

]]>

</query>

2. Use the following query:

String[] values= new string[]{"test", "123"};

This.gethibernatetemplate (). Findbynamedquery ("Querybynameandpassword", values);

Xi. Findbynamedqueryandnamedparam (String queryname, String paramname, Object value)

Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Querybyname" ><!--This query is called by name--

<! [cdata[

From Bean. User u where u.name =:myname

]]>

</query>

2. Use the following query:

This.gethibernatetemplate (). Findbynamedquery ("Querybyname", "MyName", "Test");

12, Findbynamedqueryandnamedparam (String queryname, string[] paramname, object[] value)


Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Querybynameandpassword" ><!--This query is called by name--

<! [cdata[

From Bean. User u where u.name =:myname and U.password=:mypassword

]]>

</query>

2. Use the following query:

String[] names= new string[]{"MyName", "MyPassword"};

String[] values= new string[]{"test", "123"};

This.gethibernatetemplate (). Findbynamedquery ("Querybynameandpassword", names, values);

13, Findbyvaluebean (String queryString, Object value);

Example:

1. Define a Valuebean, the property name must have the same name as the variable name that follows in the HSQL statement, there must be at least two properties, respectively, MyName and

MyPassword, after setting the property value using the Setter method

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);

14, Findbynamedqueryandvaluebean (String queryname, Object value);

Example:

1, first need to define the named query in User.hbm.xml

<class>......</class>

<query name= "Querybynameandpassword" ><!--This query is called by name--

<! [cdata[

From Bean. User u where u.name =:myname and U.password=:mypassword

]]>

</query>

2. To define a Valuebean, the property name must have the same name as the variable name that follows in the User.hbm.xml named query statement, where there must be at least two properties, respectively

For MyName and MyPassword, after setting the property value using the Setter method

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);

Source: http://blog.sina.com.cn/s/blog_643014ef0100h28r.html

Hql Query method commonly used in "Go" Spring (gethibernatetemplate ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.