Ibatis之3個不常用的Query方法,ibatisquery方法

來源:互聯網
上載者:User

Ibatis之3個不常用的Query方法,ibatisquery方法

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 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;

當查詢對象是一個重量級對象、建立過程比較複雜時或者查詢對象沒有預設的構造方法時,通過該方法,可以在外部先構建好查詢對象,然後傳給Ibatis,Ibatis此時不會建立新對象,而是調用傳入對象的set方法進行賦值。

2.queryForList

  /**   * Executes a mapped SQL SELECT statement that returns data to populate   * a number of result objects within a certain range.   * <p/>   * 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.   * <p/>   * 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裡用起來可能更方便,如果沒有這個方法還得自己進行轉換,同樣的一個<select ...>配置,不用做任何更改即可以用queryForList訪問也可以用queryForMap訪問。


 


ibatis 插入資料的問題

1.查詢型方法
 QueryForObject()方法用於從資料庫中擷取單行資料,並將其轉換為JavaBean對象。"selectUser"對應於定義在對應檔中<select>節點的id。
UserInfo user = new UserInfoVo();
user.setUserName("Andy");
UserInfo user=(UserInfo)sqlMapClient.queryForObject("selectUser", userInfo);
本次查詢的結果對象user中就得到資料庫中使用者名稱稱是“Andy”的使用者資訊。
2。QueryForList()方法用於從資料庫中擷取多行資料以List對象的形式返回,與它使用方法相同的還有QueryForMap(),返回的資料封裝在HashMap的對象中。它們也都有兩個參數,字串值對應與定義在對應檔中<select>節點的id。
使用方法如下:
UserInfo user = new UserInfo();
List list=new ArrayList();
list=sqlMapClient .queryForList("selectAllUserInfo", user);
3。queryForPaginatedList()用來進行分頁查詢,它有3個參數,分別是字串與<selelct>節點id對應、查詢條件的對象、每次查詢結果的數量。使用的方法如下:
UserInfo user=new UserInfo();
PaginatedList list= sqlMap.queryForPaginatedList("selectAllUserInfo", user, 10);

至於iBATIS的設定檔和對應檔你一定知道如何配置吧!
參考資料:《開發人員突擊 Struts2核心技術與JavaEE架構整合開發實戰》
 
ibatis(mybatis)底層封裝的常用方法有什

常用的:updateByExample、selectByPrimaryKey、selectByExample、insert、insertSelective、
deleteByPrimaryKey、deleteByExample ...基本上DAOImpl裡的都用得上;

舉個例子說明:

public List selectByExample(TUserExample example) {
List list = getSqlMapClientTemplate().queryForList("t_user.ibatorgenerated_selectByExample",example);
return list;
}
這個方法,是按指定的條件從表中查詢資料。方法參數類型TUserExample,為封裝了查詢條件的類,查詢條件會被傳到**sqlMap.xml中的sql中,查詢的一條條記錄都會被封裝成一個個的對象,然後放在一個List中返回給你。
 

相關文章

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.