⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basesqlmapdao.java

📁 Java/J2EE框架Jdon-Framework系统的源代码
💻 JAVA
字号:
/**
 * User: Clinton Begin Date: Jul 13, 2003 Time: 7:24:04 PM
 */
package com.jdon.framework.samples.jpetstore.persistence.dao.sqlmapdao;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.common.util.PaginatedList;
import com.ibatis.dao.client.DaoException;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.DaoTemplate;
import com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ibatis.sqlmap.client.SqlMapTransactionManager;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.jdon.util.Debug;

public class BaseSqlMapDao extends DaoTemplate implements SqlMapExecutor{
    private final static String module = BaseSqlMapDao.class.getName();

    /**
     * The DaoManager that manages this Dao instance will be passed
     * in as the parameter to this constructor automatically upon
     * instantiation.
     *
     * @param daoManager
     */
    public BaseSqlMapDao(DaoManager daoManager) {
      super(daoManager);
    }

    /**
     * Gets the SQL Map Executor associated with the current
     * DaoTransaction that this Dao is working under.  The SqlMapExecutor
     * interface declares a number of methods for executing statements
     * via an SqlMapClient instance.
     *
     * @return A SqlMapExecutor instance.
     */
    protected SqlMapExecutor getSqlMapExecutor() {
        Debug.logVerbose("11", module);
      SqlMapDaoTransaction trans = (SqlMapDaoTransaction) daoManager.getTransaction(this);
      Debug.logVerbose("11222", module);
      return trans.getSqlMap();
    }

    /**
     * Gets the SQL Map Transaction Manager associated with the current
     * DaoTransaction that this Dao is working under.  The SqlMapExecutor
     * interface declares a number of methods for executing statements
     * via an SqlMapClient instance.
     * <p/>
     * NOTE: It is rare to require this in a DAO.  Only very special
     * cases of DAO implementations will require access to the
     * SqlMapTransactionManager.  Messing with transactions at this
     * level might be dangerous to your data integrity (e.g. committing
     * too early).
     *
     * @return A SqlMapTransactionManager instance.
     */
    protected SqlMapTransactionManager getSqlMapTransactionManager() {
      SqlMapDaoTransaction trans = (SqlMapDaoTransaction) daoManager.getTransaction(this);
      return trans.getSqlMap();
    }

    /**
     * Executes a mapped SQL INSERT statement.
     * Insert is a bit different from other update methods, as it
     * provides facilities for returning the primary key of the
     * newly inserted row (rather than the effected rows).  This
     * functionality is of course optional.
     * <p/>
     * The parameter object is generally used to supply the input
     * data for the INSERT values.
     *
     * @param id              The name of the statement to execute.
     * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
     * @return The primary key of the newly inserted row.  This might be automatically
     *         generated by the RDBMS, or selected from a sequence table or other source.
     */
    public Object insert(String id, Object parameterObject) {
      try {
        return getSqlMapExecutor().insert(id, parameterObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to insert - id ["
            + id + "], parameterObject [" + parameterObject + "]. Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL UPDATE statement.
     * Update can also be used for any other update statement type,
     * such as inserts and deletes.  Update returns the number of
     * rows effected.
     * <p/>
     * The parameter object is generally used to supply the input
     * data for the UPDATE values as well as the WHERE clause parameter(s).
     *
     * @param id              The name of the statement to execute.
     * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
     * @return The number of rows effected.
     */
    public int update(String id, Object parameterObject) {
      try {
        return getSqlMapExecutor().update(id, parameterObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to update - id ["
            + id + "] - parameterObject [" + parameterObject + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL DELETE statement.
     * Delete returns the number of rows effected.
     * <p/>
     * The parameter object is generally used to supply the input
     * data for the WHERE clause parameter(s) of the DELETE statement.
     *
     * @param id              The name of the statement to execute.
     * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
     * @return The number of rows effected.
     */
    public int delete(String id, Object parameterObject) {
      try {
        return getSqlMapExecutor().delete(id, parameterObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to delete - id ["
            + id + "] - parameterObject [" + parameterObject + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a single object instance.
     * <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.).
     * @return The single result object populated with the result set data.
     */
    public Object queryForObject(String id, Object parameterObject) {
      try {
        return getSqlMapExecutor().queryForObject(id, parameterObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to execute queryForObject - id ["
            + id + "], parameterObject [" + parameterObject + "].  Cause: " + e, e);
      }
    }

    /**
     * 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.
     */
    public Object queryForObject(String id, Object parameterObject, Object resultObject) {
      try {
        return getSqlMapExecutor().queryForObject(id, parameterObject, resultObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForObject - id ["
            + id + "], parameterObject [" + parameterObject + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a number of result objects.
     * <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.).
     * @return A List of result objects.
     */
    public List queryForList(String id, Object parameterObject) {
      try {
        return getSqlMapExecutor().queryForList(id, parameterObject);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForList - id ["
            + id + "], parameterObject [" + parameterObject + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a number of result objects within a certain range.
     * <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 skip            The number of results to ignore.
     * @param max             The maximum number of results to return.
     * @return A List of result objects.
     */
    public List queryForList(String id, Object parameterObject, int skip, int max) {
      try {
        return getSqlMapExecutor().queryForList(id, parameterObject, skip, max);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForList - id ["
            + id + "], parameterObject [" + parameterObject + "], skip ["
            + skip + "], max [" + max + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a number of result objects that will be handled one at a time by a
     * RowHandler.
     * <p/>
     * This is generally a good approach to take when dealing with large sets
     * of records (i.e. hundreds, thousands...) that need to be processed without
     * eating up all of the system resources.
     * <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 rowHandler      A RowHandler instance
     */
    public void queryWithRowHandler(String id, Object parameterObject, RowHandler rowHandler) {
      try {
        getSqlMapExecutor().queryWithRowHandler(id, parameterObject, rowHandler);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForList - id [" + id + "], parameterObject ["
            + parameterObject + "], rowHandler [ " + rowHandler + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a number of result objects a page at a time.
     * <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 pageSize        The maximum number of result objects each page can hold.
     * @return A PaginatedList of result objects.
     */
    public PaginatedList queryForPaginatedList(String id, Object parameterObject, int pageSize) {
      try {
        return getSqlMapExecutor().queryForPaginatedList(id, parameterObject, pageSize);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForPaginatedList - id [" + id + "], parameterObject ["
            + parameterObject + "], pageSize [" + pageSize + "].  Cause: " + e, e);
      }
    }

    /**
     * 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.
     */
    public Map queryForMap(String id, Object parameterObject, String keyProp) {
      try {
        return getSqlMapExecutor().queryForMap(id, parameterObject, keyProp);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForMap - id [" + id + "], parameterObject ["
            + parameterObject + "], keyProp [" + keyProp + "].  Cause: " + e, e);
      }
    }

    /**
     * Executes a mapped SQL SELECT statement that returns data to populate
     * a number of result objects from which one property 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.
     * @param valueProp       The property to be used as the value in the Map.
     * @return A Map keyed by keyProp with values of valueProp.
     */
    public Map queryForMap(String id, Object parameterObject, String keyProp, String valueProp) {
      try {
        return getSqlMapExecutor().queryForMap(id, parameterObject, keyProp, valueProp);
      } catch (SQLException e) {
        throw new DaoException("Failed to queryForMap - id [" + id + "], parameterObject ["
            + parameterObject + "], keyProp [" + keyProp + "], valueProp ["
            + valueProp + "].  Cause: " + e, e);
      }
    }

    /**
     * Starts a batch in which update statements will be cached before being sent to
     * the database all at once. This can improve overall performance of updates update
     * when dealing with numerous updates (e.g. inserting 1:M related data).
     */
    public void startBatch() {
      try {
        getSqlMapExecutor().startBatch();
      } catch (SQLException e) {
        throw new DaoException("Failed to startBatch.  Cause: " + e, e);
      }
    }

    /**
     * Executes (flushes) all statements currently batched.
     */
    public int executeBatch() {
      try {
        return getSqlMapExecutor().executeBatch();
      } catch (SQLException e) {
        throw new DaoException("Failed to executeBatch.  Cause: " + e, e);
      }
    }
    
    public List queryForList(String id, Object parameterObject, RowHandler rowHandler)
    throws SQLException
{
    throw new UnsupportedOperationException("This method is deprecated in the SQL Maps API.");
}
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -