Hibernatetemplate provides a number of common methods for basic operations, such as additions, deletions, modifications, queries, and so on, and Spring 2.0 adds support for named SQL queries, as well as paging. In most cases, you can perform CRUD operations on most DAO objects by using the general usage of hibernate.
1. Find (String querystring);
QUERYSTRING:HQL Query Statement
String querystring = "from user";
This.gethibernatetemplate (). Find (querystring);
Return: A collection of user objects
2. Find (String querystring, Object value);
QUERYSTRING:HQL Query Statement
Value: Query criteria
String querystring = "from user u where u.name=?";
This.gethibernatetemplate (). Find (QueryString, "test");
Returns a collection of objects with the Name property value "Test"
3. Find (String querystring, object[] values);
QUERYSTRING:HQL Query Statement
Value: Array of query criteria
String hql= "from Bean. User u where u.name=? and u.password=? ";
This.gethibernatetemplate (). Find (HQL, new string[]{"test", "123"});
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
Returns a collection of objects with the Name property value "Test" and the Password property value of "123"
4. Findbyexample (Object exampleentity)
Exampleentity: Querying an instance of an object
User U=new user ();
U.setpassword ("123");
U.setname ("BB");
This.gethibernatetemplate (). Findbyexample (U);
Returns a collection of objects with the Name property value "BB" and the Password property value of "123"
This method query queries the "and" criteria of a table for SQL
5. Findbyexample (Object exampleentity, int firstresult, int maxresults)
Exampleentity: Querying an instance of an object
Firstresult: Number of rows from query results
Maxresults: Total number of rows in query results
User U=new User ();
U.setpassword ("123");
U.setname ("cc");
int start = 0;
int max = ten;
This.gethibernatetemplate (). Findbyexample (U,start,max);
Returns: The Name property value is "CC" and the Password property value is "123", since 0 a total of 10 objects (objects starting from 0 count)
6. Findbynamedparam (String querystring, String paramname, Object value)
QUERYSTRING:HQL Query Statement
ParamName: Names of reference placeholders in query criteria
Value: Values that refer to placeholders
String querystring = "SELECT COUNT (*) from user u where u.name=:myname";
String paramname= "MyName";
String value= "Xiyue";
This.gethibernatetemplate (). Findbynamedparam (QueryString, paramname, value);
Returns a collection of objects with the Name property value of "Xiyue"
7. Findbynamedparam (String querystring, string[] paramnames, object[] values)
QUERYSTRING:HQL Query Statement
Paramnames: An array of names referencing placeholders in query criteria
Values: An array of values that refer to placeholders
String querystring = "SELECT COUNT (*) from 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, Paramnames, values);
Returns: The Name property value is "Xiyue" and the collection of objects with password property value of "123"
GetSession () and Gethibernatetemplate ()
1. Using the GetSession () method you only have to inherit the sessionfactory and use the Gethibernatetemplate () method must inherit Hibernatedaosupport of course including sessionfactory
2. The GetSession () method is not wrapped in spring, and spring will give you the most original session, and you must call the Close method yourself after you use it, and you will not be able to manage the declarative transaction, once the connection is not closed in time, Causes the connection number of the database connection pool to overflow, and the Gethibernatetemplate () method is encapsulated by spring, such as adding the appropriate declarative transaction management and spring managing the appropriate connection.
3. In addition, because HIBERNATE4 has been fully able to achieve the transaction with the spring3.1 in the hibernatedao,hibernatetemplete and other conflicts, Therefore, from the spring3.1 has not provided hibernatedaosupport,hibernatetemplete, can only use the Hibernate original way with GetSession ().