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

📄 sqlmapdaotemplate.java

📁 本套系统采用了业界当前最为流行的beanAction组件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright 2004 Clinton Begin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ibatis.dao.client.template;

import com.ibatis.common.util.PaginatedList;
import com.ibatis.dao.client.DaoException;
import com.ibatis.dao.client.DaoManager;
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.ibatis.sqlmap.engine.execution.BatchException;

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

/**
 * A DaoTemplate for SQL Map implementations that provides a
 * convenient method to access the SqlMapExecutor.  This class
 * also provides SqlMapExecutor method wrappers that conveniently
 * wrap SQLExceptions with DAO Exceptions.
 *
 * @author Zach Scott
 */
public abstract class SqlMapDaoTemplate extends DaoTemplate implements SqlMapExecutor {

  /**
   * The DaoManager that manages this Dao instance will be passed
   * in as the parameter to this constructor automatically upon
   * instantiation.
   *
   * @param daoManager
   */
  public SqlMapDaoTemplate(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() {
    SqlMapDaoTransaction trans = (SqlMapDaoTransaction) daoManager.getTransaction(this);
    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 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/>
   * This overload assumes no parameter is needed.
   *
   * @param id              The name of the statement to execute.
   * @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) {
    try {
      return getSqlMapExecutor().insert(id);
    } catch (SQLException e) {
      throw new DaoException("Failed to insert - id ["
          + id + "]. 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 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/>
   * This overload assumes no parameter is needed.
   *
   * @param id              The name of the statement to execute.
   * @return The number of rows effected.
   */
  public int update(String id) {
    try {
      return getSqlMapExecutor().update(id);
    } catch (SQLException e) {
      throw new DaoException("Failed to update - id ["
          + id + "].  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 DELETE statement.
   * Delete returns the number of rows effected.
   * <p/>
   * This overload assumes no parameter is needed.
   *
   * @param id              The name of the statement to execute.
   * @return The number of rows effected.
   */
  public int delete(String id) {
    try {
      return getSqlMapExecutor().delete(id);
    } catch (SQLException e) {
      throw new DaoException("Failed to delete - id ["
          + id + "].  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
   * a single object instance.
   * <p/>
   * This overload assumes no parameter is needed.
   *
   * @param id              The name of the statement to execute.
   * @return The single result object populated with the result set data.
   */
  public Object queryForObject(String id) {
    try {
      return getSqlMapExecutor().queryForObject(id);
    } catch (SQLException e) {
      throw new DaoException("Failed to execute queryForObject - id ["
          + id + "].  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);

⌨️ 快捷键说明

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