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;
當查詢對象是一個重量級對象、創(chuàng)建過程比較復雜時或者查詢對象沒有默認的構造方法時,通過該方法,可以在外部先構建好查詢對象,然后傳給Ibatis,Ibatis此時不會創(chuàng)建新對象,而是調用傳入對象的set方法進行賦值。
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;
利用這個方法可以實現分頁功能,如(skip=0,max=10)返回前10條數據,(skip=10,max=10)返回第10-20條數據,但這個方法的分頁效率非常低,因為Ibatis是把所有的查詢結果查詢出來之后才進行篩選操作。數據量小的時候用用還可以,所以這個方法比較雞肋。
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;
網上有不少帖子說這個方法只能返回一條記錄是不對的,還有說是把resultClass的所有屬性放到一個map中返回來也是不對的。這個方法是對queryForList的一個補充,大部分情況下我們用的都是queryForList返回對象的列表,但有時候放到Map里用起來可能更方便,如果沒有這個方法還得自己進行轉換,同樣的一個
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com