Mybatis/Ibatis,数据库操作的返回值

该问题,我百度了下,根本没发现什么有价值的文章;还是看源代码(详见最后附录)中的注释,最有效了!
insert,返回值是:新插入行的主键(primary key);需要包含<selectKey>语句,才会返回主键,否则返回值为null。
update/delete,返回值是:更新或删除的行数;无需指明resultClass;但如果有约束异常而删除失败,只能去捕捉异常。
queryForObject,返回的是:一个实例对象或null;需要包含<select>语句,并且指明resultMap;
queryForList,返回的是:实例对象的列表;需要包含<select>语句,并且指明resultMap;

我的配置文件如下(desktop_common_sqlMap.xml):

  1. <typeAlias alias="UnlockTagInfo" type="com.desktop.common.bean.UnlockTagInfo" />  
  2. <resultMap class="UnlockTagInfo" id="UnlockTagInfoResult">  
  3.     <result column="id" property="id" jdbcType="INTEGER" />  
  4.     <result column="name" property="name" jdbcType="VARCHAR" />  
  5.     <result column="description" property="description" jdbcType="VARCHAR" />  
  6.     <result column="priority" property="priority" jdbcType="INTEGER" />  
  7. </resultMap>  
  8. <insert id="insertUnlockTagInfo" parameterClass="map">  
  9.     <selectKey resultClass="int" keyProperty="id">  
  10.         select  
  11.         nextval('desktop_unlock_tag_id_seq') as id  
  12.     </selectKey>  
  13.     insert into  
  14.     desktop_unlock_tag(id,name,description,priority)  
  15.     values(#id:INTEGER#,#name:VARCHAR#,#description:VARCHAR#,#priority:INTEGER#)  
  16. </insert>  
  17. <update id="updateUnlockTagInfo" parameterClass="map">  
  18.     update  
  19.     desktop_unlock_tag  
  20.     set modify_time=now(),priority=#priority:INTEGER#,  
  21.     name=#name:VARCHAR#,description=#description:VARCHAR#  
  22.     where  
  23.     id=#id:INTEGER#  
  24. </update>  
  25. <delete id="deleteUnlockTagInfo" parameterClass="int">  
  26.     delete from  
  27.     desktop_unlock_tag  
  28.     where id=#value:INTEGER#  
  29. </delete>  
  30. <select id="countUnlockTagInfo" resultClass="int">  
  31.     select count(*)  
  32.     from  
  33.     desktop_unlock_tag  
  34. </select>  
  35. <sql id="selectUnlockTagInfo">  
  36.     select  
  37.     id,name,description,priority  
  38.     from  
  39.     desktop_unlock_tag  
  40. </sql>  
  41. <select id="findUnlockTagInfoById" parameterClass="int"  
  42.     resultMap="UnlockTagInfoResult">  
  43.     <include refid="selectUnlockTagInfo" />  
  44.     where id=#id:INTEGER#  
  45. </select>  
  46. <select id="listUnlockTagInfo" parameterClass="map"  
  47.     resultMap="UnlockTagInfoResult">  
  48.     <include refid="selectUnlockTagInfo" />  
  49.     order by  
  50.     modify_time desc limit #size:INTEGER#  
  51.     offset #start:INTEGER#  
  52. </select>  
我的DAO源码如下:
  1. public class UnlockTagDaoImpl extends SqlMapClientDaoSupport implements  
  2.         UnlockTagDao {  
  3.     @Override  
  4.     public Integer addItem(String name, String desc, Integer priority) {  
  5.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  6.         Map<String, Object> args = new HashMap<String, Object>();  
  7.         args.put("name", name);  
  8.         args.put("description", desc);  
  9.         args.put("priority", priority);  
  10.         Object key = template.insert("DesktopCommon.insertUnlockTagInfo", args);  
  11.         return (Integer) key;  
  12.     }  
  13.   
  14.     @Override  
  15.     public boolean updateItem(Integer id, String name, String description,  
  16.             Integer priority) {  
  17.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  18.         Map<String, Object> args = new HashMap<String, Object>();  
  19.         args.put("id", id);  
  20.         args.put("name", name);  
  21.         args.put("description", description);  
  22.         args.put("priority", priority);  
  23.         try {  
  24.             int c = template.update("DesktopCommon.updateUnlockTagInfo", args);  
  25.             if (c > 0) {  
  26.                 return true;  
  27.             }  
  28.             return false;  
  29.         } catch (Exception e) {  
  30.             return false;  
  31.         }  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean deleteItem(Integer id) {  
  36.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  37.         try {  
  38.             int c = template.delete("DesktopCommon.deleteUnlockTagInfo", id);  
  39.             if (c > 0) {  
  40.                 return true;  
  41.             }  
  42.             return false;  
  43.         } catch (Exception e) {  
  44.             return false;  
  45.         }  
  46.     }  
  47.   
  48.     @Override  
  49.     public UnlockTagInfo findItemById(Integer id) {  
  50.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  51.         UnlockTagInfo item = (UnlockTagInfo) template.queryForObject(  
  52.                 "DesktopCommon.findUnlockTagInfoById", id);  
  53.         return item;  
  54.     }  
  55.   
  56.     @Override  
  57.     public PagedList<UnlockTagInfo> listAll(Integer nStart, Integer nSize,  
  58.             boolean bCountTotal) {  
  59.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  60.         PagedList<UnlockTagInfo> result = new PagedList<UnlockTagInfo>();  
  61.         if (bCountTotal) {  
  62.             int total = (Integer) template  
  63.                     .queryForObject("DesktopCommon.countUnlockTagInfo");  
  64.             result.setTotal(total);  
  65.         }  
  66.         Map<String, Integer> args = new HashMap<String, Integer>();  
  67.         args.put("start", nStart);  
  68.         args.put("size", nSize);  
  69.         @SuppressWarnings("unchecked")  
  70.         List<UnlockTagInfo> items = template.queryForList(  
  71.                 "DesktopCommon.listUnlockTagInfo", args);  
  72.         result.setData(items);  
  73.         return result;  
  74.     }  
  75. }  

关于ibatis的接口,参见其源码(comibatissqlmapclientSqlMapExecutor.java):

  1. /* 
  2.  *  Copyright 2004 Clinton Begin 
  3.  * 
  4.  *  Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  *  you may not use this file except in compliance with the License. 
  6.  *  You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  *  Unless required by applicable law or agreed to in writing, software 
  11.  *  distributed under the License is distributed on an "AS IS" BASIS, 
  12.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  *  See the License for the specific language governing permissions and 
  14.  *  limitations under the License. 
  15.  */  
  16. package com.ibatis.sqlmap.client;  
  17.   
  18. import com.ibatis.common.util.PaginatedList;  
  19. import com.ibatis.sqlmap.client.event.RowHandler;  
  20. import com.ibatis.sqlmap.engine.execution.BatchException;  
  21.   
  22. import java.sql.SQLException;  
  23. import java.util.List;  
  24. import java.util.Map;  
  25.   
  26. /** 
  27.  * This interface declares all methods involved with executing statements 
  28.  * and batches for an SQL Map. 
  29.  * 
  30.  * @see SqlMapSession 
  31.  * @see SqlMapClient 
  32.  */  
  33. public interface SqlMapExecutor {  
  34.   
  35.   /** 
  36.    * Executes a mapped SQL INSERT statement. 
  37.    * Insert is a bit different from other update methods, as it 
  38.    * provides facilities for returning the primary key of the 
  39.    * newly inserted row (rather than the effected rows).  This 
  40.    * functionality is of course optional. 
  41.    * <p/> 
  42.    * The parameter object is generally used to supply the input 
  43.    * data for the INSERT values. 
  44.    * 
  45.    * @param id              The name of the statement to execute. 
  46.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  47.    * @return The primary key of the newly inserted row.  This might be automatically 
  48.    *         generated by the RDBMS, or selected from a sequence table or other source. 
  49.    * @throws java.sql.SQLException If an error occurs. 
  50.    */  
  51.   Object insert(String id, Object parameterObject) throws SQLException;  
  52.   
  53.   /** 
  54.    * Executes a mapped SQL INSERT statement. 
  55.    * Insert is a bit different from other update methods, as it 
  56.    * provides facilities for returning the primary key of the 
  57.    * newly inserted row (rather than the effected rows).  This 
  58.    * functionality is of course optional. 
  59.    * <p/> 
  60.    * This overload assumes no parameter is needed. 
  61.    * 
  62.    * @param id              The name of the statement to execute. 
  63.    * @return The primary key of the newly inserted row.  This might be automatically 
  64.    *         generated by the RDBMS, or selected from a sequence table or other source. 
  65.    * @throws java.sql.SQLException If an error occurs. 
  66.    */  
  67.   Object insert(String id) throws SQLException;  
  68.   
  69.   /** 
  70.    * Executes a mapped SQL UPDATE statement. 
  71.    * Update can also be used for any other update statement type, 
  72.    * such as inserts and deletes.  Update returns the number of 
  73.    * rows effected. 
  74.    * <p/> 
  75.    * The parameter object is generally used to supply the input 
  76.    * data for the UPDATE values as well as the WHERE clause parameter(s). 
  77.    * 
  78.    * @param id              The name of the statement to execute. 
  79.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  80.    * @return The number of rows effected. 
  81.    * @throws java.sql.SQLException If an error occurs. 
  82.    */  
  83.   int update(String id, Object parameterObject) throws SQLException;  
  84.   
  85.   /** 
  86.    * Executes a mapped SQL UPDATE statement. 
  87.    * Update can also be used for any other update statement type, 
  88.    * such as inserts and deletes.  Update returns the number of 
  89.    * rows effected. 
  90.    * <p/> 
  91.    * This overload assumes no parameter is needed. 
  92.    * 
  93.    * @param id              The name of the statement to execute. 
  94.    * @return The number of rows effected. 
  95.    * @throws java.sql.SQLException If an error occurs. 
  96.    */  
  97.   int update(String id) throws SQLException;  
  98.   
  99.   /** 
  100.    * Executes a mapped SQL DELETE statement. 
  101.    * Delete returns the number of rows effected. 
  102.    * <p/> 
  103.    * The parameter object is generally used to supply the input 
  104.    * data for the WHERE clause parameter(s) of the DELETE statement. 
  105.    * 
  106.    * @param id              The name of the statement to execute. 
  107.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  108.    * @return The number of rows effected. 
  109.    * @throws java.sql.SQLException If an error occurs. 
  110.    */  
  111.   int delete(String id, Object parameterObject) throws SQLException;  
  112.   
  113.   /** 
  114.    * Executes a mapped SQL DELETE statement. 
  115.    * Delete returns the number of rows effected. 
  116.    * <p/> 
  117.    * This overload assumes no parameter is needed. 
  118.    * 
  119.    * @param id              The name of the statement to execute. 
  120.    * @return The number of rows effected. 
  121.    * @throws java.sql.SQLException If an error occurs. 
  122.    */  
  123.   int delete(String id) throws SQLException;  
  124.   
  125.   /** 
  126.    * Executes a mapped SQL SELECT statement that returns data to populate 
  127.    * a single object instance. 
  128.    * <p/> 
  129.    * The parameter object is generally used to supply the input 
  130.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  131.    * 
  132.    * @param id              The name of the statement to execute. 
  133.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  134.    * @return The single result object populated with the result set data, 
  135.    *         or null if no result was found 
  136.    * @throws java.sql.SQLException If more than one result was found, or if any other error occurs. 
  137.    */  
  138.   Object queryForObject(String id, Object parameterObject) throws SQLException;  
  139.   
  140.   /** 
  141.    * Executes a mapped SQL SELECT statement that returns data to populate 
  142.    * a single object instance. 
  143.    * <p/> 
  144.    * This overload assumes no parameter is needed. 
  145.    * 
  146.    * @param id              The name of the statement to execute. 
  147.    * @return The single result object populated with the result set data, 
  148.    *         or null if no result was found 
  149.    * @throws java.sql.SQLException If more than one result was found, or if any other error occurs. 
  150.    */  
  151.   Object queryForObject(String id) throws SQLException;  
  152.   
  153.   /** 
  154.    * Executes a mapped SQL SELECT statement that returns data to populate 
  155.    * the supplied result object. 
  156.    * <p/> 
  157.    * The parameter object is generally used to supply the input 
  158.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  159.    * 
  160.    * @param id              The name of the statement to execute. 
  161.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  162.    * @param resultObject    The result object instance that should be populated with result data. 
  163.    * @return The single result object as supplied by the resultObject parameter, populated with the result set data, 
  164.    *         or null if no result was found 
  165.    * @throws java.sql.SQLException If more than one result was found, or if any other error occurs. 
  166.    */  
  167.   Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException;  
  168.   
  169.   /** 
  170.    * Executes a mapped SQL SELECT statement that returns data to populate 
  171.    * a number of result objects. 
  172.    * <p/> 
  173.    * The parameter object is generally used to supply the input 
  174.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  175.    * 
  176.    * @param id              The name of the statement to execute. 
  177.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  178.    * @return A List of result objects. 
  179.    * @throws java.sql.SQLException If an error occurs. 
  180.    */  
  181.   List queryForList(String id, Object parameterObject) throws SQLException;  
  182.   
  183.   /** 
  184.    * Executes a mapped SQL SELECT statement that returns data to populate 
  185.    * a number of result objects. 
  186.    * <p/> 
  187.    * This overload assumes no parameter is needed. 
  188.    * 
  189.    * @param id              The name of the statement to execute. 
  190.    * @return A List of result objects. 
  191.    * @throws java.sql.SQLException If an error occurs. 
  192.    */  
  193.   List queryForList(String id) throws SQLException;  
  194.   
  195.   /** 
  196.    * Executes a mapped SQL SELECT statement that returns data to populate 
  197.    * a number of result objects within a certain range. 
  198.    * <p/> 
  199.    * The parameter object is generally used to supply the input 
  200.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  201.    * 
  202.    * @param id              The name of the statement to execute. 
  203.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  204.    * @param skip            The number of results to ignore. 
  205.    * @param max             The maximum number of results to return. 
  206.    * @return A List of result objects. 
  207.    * @throws java.sql.SQLException If an error occurs. 
  208.    */  
  209.   List queryForList(String id, Object parameterObject, int skip, int max) throws SQLException;  
  210.   
  211.   /** 
  212.    * Executes a mapped SQL SELECT statement that returns data to populate 
  213.    * a number of result objects within a certain range. 
  214.    * <p/> 
  215.    * This overload assumes no parameter is needed. 
  216.    * 
  217.    * @param id              The name of the statement to execute. 
  218.    * @param skip            The number of results to ignore. 
  219.    * @param max             The maximum number of results to return. 
  220.    * @return A List of result objects. 
  221.    * @throws java.sql.SQLException If an error occurs. 
  222.    */  
  223.   List queryForList(String id, int skip, int max) throws SQLException;  
  224.     
  225.   /** 
  226.    * Executes a mapped SQL SELECT statement that returns a number of 
  227.    * result objects that will be handled one at a time by a 
  228.    * RowHandler. 
  229.    * <p/> 
  230.    * This is generally a good approach to take when dealing with large sets 
  231.    * of records (i.e. hundreds, thousands...) that need to be processed without 
  232.    * eating up all of the system resources. 
  233.    * <p/> 
  234.    * The parameter object is generally used to supply the input 
  235.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  236.    * 
  237.    * @param id              The name of the statement to execute. 
  238.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  239.    * @param rowHandler      A RowHandler instance 
  240.    * @throws java.sql.SQLException If an error occurs. 
  241.    */  
  242.   void queryWithRowHandler(String id, Object parameterObject, RowHandler rowHandler) throws SQLException;  
  243.   
  244.   /** 
  245.    * Executes a mapped SQL SELECT statement that returns a number of 
  246.    * result objects that will be handled one at a time by a 
  247.    * RowHandler. 
  248.    * <p/> 
  249.    * This is generally a good approach to take when dealing with large sets 
  250.    * of records (i.e. hundreds, thousands...) that need to be processed without 
  251.    * eating up all of the system resources. 
  252.    * <p/> 
  253.    * This overload assumes no parameter is needed. 
  254.    * 
  255.    * @param id              The name of the statement to execute. 
  256.    * @param rowHandler      A RowHandler instance 
  257.    * @throws java.sql.SQLException If an error occurs. 
  258.    */  
  259.   void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException;  
  260.   
  261.   /** 
  262.    * Executes a mapped SQL SELECT statement that returns data to populate 
  263.    * a number of result objects a page at a time. 
  264.    * <p/> 
  265.    * The parameter object is generally used to supply the input 
  266.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  267.    * 
  268.    * @param id              The name of the statement to execute. 
  269.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  270.    * @param pageSize        The maximum number of result objects each page can hold. 
  271.    * @return A PaginatedList of result objects. 
  272.    * @throws java.sql.SQLException If an error occurs. 
  273.    * @deprecated All paginated list features have been deprecated 
  274.    */  
  275.   PaginatedList queryForPaginatedList(String id, Object parameterObject, int pageSize) throws SQLException;  
  276.   
  277.   /** 
  278.    * Executes a mapped SQL SELECT statement that returns data to populate 
  279.    * a number of result objects a page at a time. 
  280.    * <p/> 
  281.    * This overload assumes no parameter is needed. 
  282.    * 
  283.    * @param id              The name of the statement to execute. 
  284.    * @param pageSize        The maximum number of result objects each page can hold. 
  285.    * @return A PaginatedList of result objects. 
  286.    * @throws java.sql.SQLException If an error occurs. 
  287.    * @deprecated All paginated list features have been deprecated 
  288.    */  
  289.   PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException;  
  290.   
  291.   /** 
  292.    * Executes a mapped SQL SELECT statement that returns data to populate 
  293.    * a number of result objects that will be keyed into a Map. 
  294.    * <p/> 
  295.    * The parameter object is generally used to supply the input 
  296.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  297.    * 
  298.    * @param id              The name of the statement to execute. 
  299.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  300.    * @param keyProp         The property to be used as the key in the Map. 
  301.    * @return A Map keyed by keyProp with values being the result object instance. 
  302.    * @throws java.sql.SQLException If an error occurs. 
  303.    */  
  304.   Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException;  
  305.   
  306.   /** 
  307.    * Executes a mapped SQL SELECT statement that returns data to populate 
  308.    * a number of result objects from which one property will be keyed into a Map. 
  309.    * <p/> 
  310.    * The parameter object is generally used to supply the input 
  311.    * data for the WHERE clause parameter(s) of the SELECT statement. 
  312.    * 
  313.    * @param id              The name of the statement to execute. 
  314.    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.). 
  315.    * @param keyProp         The property to be used as the key in the Map. 
  316.    * @param valueProp       The property to be used as the value in the Map. 
  317.    * @return A Map keyed by keyProp with values of valueProp. 
  318.    * @throws java.sql.SQLException If an error occurs. 
  319.    */  
  320.   Map queryForMap(String id, Object parameterObject, String keyProp, String valueProp) throws SQLException;  
  321.   
  322.   /** 
  323.    * Starts a batch in which update statements will be cached before being sent to 
  324.    * the database all at once. This can improve overall performance of updates update 
  325.    * when dealing with numerous updates (e.g. inserting 1:M related data). 
  326.    * 
  327.    * @throws java.sql.SQLException If the batch could not be started. 
  328.    */  
  329.   void startBatch() throws SQLException;  
  330.   
  331.   /** 
  332.    * Executes (flushes) all statements currently batched. 
  333.    * 
  334.    * @return the number of rows updated in the batch 
  335.    * @throws java.sql.SQLException If the batch could not be executed or if any of the statements 
  336.    *                               fails. 
  337.    */  
  338.   int executeBatch() throws SQLException;  
  339.   
  340.   /** 
  341.    * Executes (flushes) all statements currently batched. 
  342.    * 
  343.    * @return a List of BatchResult objects.  There will be one element in the 
  344.    *  list for each sub-batch executed.  A sub-batch is created by adding a statement 
  345.    *  to the batch that does not equal the prior statement.  
  346.    * @throws SQLException if a database access error occurs, or the drive 
  347.    *   does not support batch statements 
  348.    * @throws BatchException if the driver throws BatchUpdateException 
  349.    * @see com.ibatis.sqlmap.engine.execution.BatchException 
  350.    */  
  351.   List executeBatchDetailed() throws SQLException, BatchException;  
  352. }  




原文地址:https://www.cnblogs.com/jpfss/p/8918328.html