<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Ibatis之3個不常用的Query方法

        來源:懂視網 責編:小采 時間:2020-11-09 14:36:36
        文檔

        Ibatis之3個不常用的Query方法

        Ibatis之3個不常用的Query方法: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
        推薦度:
        導讀Ibatis之3個不常用的Query方法: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. * 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里用起來可能更方便,如果沒有這個方法還得自己進行轉換,同樣的一個