Use of the Hibernatetemplate class
Hibernatetemplate provides a number of common ways to perform basic operations, such as the usual additions, deletions, modifications, queries, and more, Spring 2.0 adds support for named SQL queries, and also adds support for paging. In most cases, the CRUD operations of most DAO objects can be done using Hibernate's general usage.
1. . Find (String queryString);
QUERYSTRING:HQL Query Statements
String queryString = "from user"; This.gethibernatetemplate (). Find (queryString);
Return: Collection of User objects
2: Find (String queryString, Object value);
QUERYSTRING:HQL Query Statements
Value: Query criteria
String queryString = "from user u where u.name=?"; This.gethibernatetemplate (). Find (queryString, "test");
Return: The collection of objects with the Name property value "Test"
3: Find (String queryString, object[] values);
QUERYSTRING:HQL Query Statements
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"});
Return: A collection of objects with the Name property value "Test" and the Password property value "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);
Return: The Name property value is "BB" and the Password property value is "123" for the collection of objects
This method query is the "and" condition query for SQL to look up a table
5. . Findbyexample (Object exampleentity, int firstresult, int maxResults)
Exampleentity: Querying an instance of an object
Firstresult: The starting line number of the query result
MaxResults: Total number of rows for query results
User U=new User (); U.setpassword ("123"); U.setname ("CC"); int start = 0;int max = 10;this.gethibernatetemplate (). Findbyexample (U,start, MAX);
Return: The Name property value is "CC" and the Password property value is "123", since 0 a collection of 10 objects (objects counting from 0)
6. . Findbynamedparam (String queryString, String paramname, Object value)
QUERYSTRING:HQL Query Statements
ParamName: Name of the reference placeholder in the query condition
Value: reference to placeholder values
Return: A collection of objects with the Name property value "Xiyue"
7. . Findbynamedparam (String queryString, string[] paramnames, object[] values)
QUERYSTRING:HQL Query Statements
Paramnames: An array of names that reference placeholders in the query condition
Values: An array that refers to the value of a placeholder
Return: The Name property value is "Xiyue", and the Password property value is "123" for the collection of objects
GetSession () and Gethibernatetemplate ()
1. Use the GetSession () method as long as you inherit Sessionfactory, and the Gethibernatetemplate () method must inherit Hibernatedaosupport of course include sessionfactory
2. The GetSession () method is not wrapped in spring, spring will give you the most primitive session, after use, you must call the corresponding Close method, and do not manage the declarative transaction, once the connection is not closed in a timely manner, Can overflow the number of connections to the database connection pool, and the Gethibernatetemplate () method is spring encapsulated, such as adding the appropriate declarative transaction management, which manages the corresponding connection by spring.
3. In addition, since HIBERNATE4 has been fully able to achieve transactions with the spring3.1 in the hibernatedao,hibernatetemplete and other conflicts, So from the spring3.1 has not provided the Hibernatedaosupport,hibernatetemplete, can only use Hibernate original way with GetSession ().
Use of the Hibernatetemplate class