Ibatis: Three uncommon Query methods

Source: Internet
Author: User
1. queryForObject *** implements * thesuppliedresultobject. * p * Theparameterobjectisgenerallyusedtosupplytheinput * datafortheWHEREclauseparameter (s) oftheSEL

1. queryForObject/*** Executes a mapped SQL SELECT statement that returns data to populate * the supplied result object. * p/* The parameter object is generally used to supply the input * data for the WHERE clause parameter (s) of the SEL

1. queryForObject

  /**   * Executes a mapped SQL SELECT statement that returns data to populate   * the supplied result object.   * 

* The parameter object is generally used to supply the input * data for the WHERE clause parameter(s) of the SELECT statement. * * @param id The name of the statement to execute. * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). * @param resultObject The result object instance that should be populated with result data. * @return The single result object as supplied by the resultObject parameter, populated with the result set data, * or null if no result was found * @throws java.sql.SQLException If more than one result was found, or if any other error occurs. */ Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException;

When the query object is a heavyweight Object and the creation process is complex, or the query object does not have a default constructor method, you can use this method to create a query object externally and then send it to Ibatis, ibatis does not create a new object at this time, but calls the set Method of the input object to assign values.

2. queryForList

  /**   * Executes a mapped SQL SELECT statement that returns data to populate   * a number of result objects within a certain range.   * 

* This overload assumes no parameter is needed. * * @param id The name of the statement to execute. * @param skip The number of results to ignore. * @param max The maximum number of results to return. * @return A List of result objects. * @throws java.sql.SQLException If an error occurs. */ List queryForList(String id, int skip, int max) throws SQLException;

This method can be used to implement the paging function. For example, (skip = 0, max = 10), the first 10 data records are returned (skip = 10, max = 10), and 10-20 data records are returned, however, the paging efficiency of this method is very low, because Ibatis performs the filtering operation only after all the query results are queried. When the data volume is small, it can still be used. Therefore, this method is quite helpful.

3. queryForMap

/**   * Executes a mapped SQL SELECT statement that returns data to populate   * a number of result objects that will be keyed into a Map.   * 

* The parameter object is generally used to supply the input * data for the WHERE clause parameter(s) of the SELECT statement. * * @param id The name of the statement to execute. * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). * @param keyProp The property to be used as the key in the Map. * @return A Map keyed by keyProp with values being the result object instance. * @throws java.sql.SQLException If an error occurs. */ Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException;

There are many posts on the internet saying that this method can only return one record. It is also wrong to put all the attributes of resultClass into a map and return it. This method is a supplement to queryForList. In most cases, queryForList is used to return the list of objects, but sometimes it is more convenient to put it in Map, if you do not have this method, You have to convert it yourself.Configuration, which can be accessed using queryForList or queryForMap without any changes.

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.