Ibatis learning summary 7 -- SqlMapClient executes the SQL statement, ibatissqlmapclient
The SqlMapCient class provides APIs for executing all mapped statement. These methods are as follows:
1 public int insert(String statementName, Object parameterObject) 2 throws SQLException 3 4 5 6 7 public int update(String statementName, Object parameterObject) 8 throws SQLException 9 10 11 public int delete(String statementName, Object parameterObject)12 throws SQLException13 14 15 public Object queryForObject(String statementName, Object parameterObject)16 throws SQLException17 18 public Object queryForObject(String statementName, Object parameterObject, Object resultObject) throws SQLException19 20 21 public List queryForList(String statementName, Object parameterObject)22 throws SQLException23 24 25 public List queryForList(String statementName, Object parameterObject, int skipResults, int maxResults)26 throws SQLException27 28 29 public List queryForList (String statementName,30 31 Object parameterObject, RowHandler rowHandler)32 33 throws SQLException34 35 36 public PaginatedList queryForPaginatedList(String statementName, Object parameterObject, int pageSize)37 throws SQLException38 39 40 public Map queryForMap (String statementName, Object parameterObject, String keyProperty)41 throws SQLException42 43 44 public Map queryForMap (String statementName, Object parameterObject, String keyProperty, String valueProperty)45 throws SQLException
In each of the preceding methods, the Mapped Statement name is used as the first parameter. This name corresponds to the name attribute of <statement>. In addition, the second parameter is always a parameter object. If you do not need a parameter object, you can
Null. Except for this, the methods above are not the same. The following describes their differences.
* Insert (), update (), delete ()
These methods are used for data update (non-query statements ). This means that using the following query method to update data is not impossible. But it seems strange, and obviously depends on the JDBC driver. Execute update () to return the number of affected data records.
* QueryForObject ()
The queryForObject () method has two versions. One returns the new query object, and the other uses a previously generated object as the parameter. The latter is useful for assigning values to objects using multiple queries.
* QueryForList ()
The queryForList () method also has three versions. The first one executes a query and returns all the query results. The second option allows you to specify the number of skipped results (that is, the start point) and the maximum number of returned results. It is useful when querying a large number of result sets and does not want to return all data. In the last version, the queryForList () method requires a row processor (row handler) as the input parameter. This method allows you to use the result object instead of the rows and columns of the data table to process the query result set row by row. In addition to routine names and parameter objects, the parameters of this method also include an instance of classes that implement the RowHandler interface. The RowHandler interface has only one method:
Public void handleRow (Object object, List list); For each row of data returned from the database, the RowHandler interface method will be executed. This method is concise and scalable.
* QueryForPaginatedList ()
The queryForPaginatedList () method is useful for returning a subset of data that can flip forward and backward. It returns a manageable List object. This interface is usually used to display only a portion of the query results. A common example is that the search engine finds 10000 results, but only 100 results are displayed at a time. The PaginatedList interface package contains methods for turning pages forward and backward (nextPage (), previousPage (), and gotoPage (), and provides methods for checking the page turning status (isFirstPage (), isMiddlePage (), isLastPage (), isPreviousPageAvailable (), getPageIndex (), getPageSize ()). Although the total number of query result sets cannot be obtained from the PaginatedList interface, the total number can be obtained by executing a simple statement count. Otherwise, the PaginatedList interface will greatly reduce the performance.
* QueryForMap ()
The queryForMap () method places the result set in a Map object, which uses an input parameter keyProperty as the key value. For example, to read a batch of Employee objects, you can place these Employee objects in a Map object that uses the employeeNumber attribute as the key value. The value of the Map object can be the entire Employee object, or another property of the Employee object. The attribute name is specified by the second parameter valueProperty. For example, you may only need a Map object. The employee ID is used as the key value, and the employee name is used as the value. Do not confuse it with the concept of using Map as the result object. This method can use Java Bean and Map (or basic type of packaging class, but it cannot be used) as the result object.
Code example:
Example 1: Execute update (insert, update, delete)
1 sqlMap.startTransaction();2 Product product = new Product(); product.setId (1); product.setDescription (“Shih Tzu”);3 int rows = sqlMap.insert (“insertProduct”, product);4 sqlMap.commitTransaction();
Example 2: select)
1 sqlMap.startTransaction();2 Integer key = new Integer (1);3 Product product = (Product)sqlMap.queryForObject (“getProduct”, key); sqlMap.commitTransaction();
Example 3: Use a pre-assigned result object to query an object (select)
1 sqlMap.startTransaction();2 Customer customer = new Customer(); sqlMap.queryForObject(“getCust”, parameterObject, customer);
sqlMap.queryForObject(“getAddr”, parameterObject, customer); sqlMap.commitTransaction();
Example 4: Query an Object List (select)
1 sqlMap.startTransaction();2 List list = sqlMap.queryForList (“getProductList”, null);3 sqlMap.commitTransaction();
Example 5: Query an Object List (select) using the result set boundary)
1 sqlMap.startTransaction();2 List list = sqlMap.queryForList (“getProductList”, null, 0, 40); sqlMap.commitTransaction();
Example 6: Use RowHandler to execute a query (select)
1 public class MyRowHandler implements RowHandler { 2 public void handleRow (Object object, List list) throws SQLException { Product product = (Product) object; 3 product.setQuantity (10000); 4 sqlMap.update (“updateProduct”, product); 5 // Optionally you could add the result object to the list. 6 // The list is returned from the queryForList() method. 7 } 8 } 9 sqlMap.startTransaction();10 RowHandler rowHandler = new MyRowHandler();11 List list = sqlMap.queryForList (“getProductList”, null, rowHandler);12 sqlMap.commitTransaction();
Example 7: Query Paginated List (select)
1 PaginatedList list =2 sqlMap.queryForPaginatedList (“getProductList”, null, 10);3 list.nextPage(); list.previousPage();
Example 9: Map (select)
1 sqlMap.startTransaction();2 Map map = sqlMap.queryForMap (“getProductList”, null, “productCode”);3 sqlMap.commitTransaction();4 Product p = (Product) map.get(“EST-93”);
Note: automatic submission. If startTransaction is not called, statements will be automatically submitted. No need for commit/rollback.