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

📄 basedataaccessobject.java

📁 以前做的一个j2ee的项目
💻 JAVA
字号:
package gov.gdlt.ssgly.taxcore.comm.dao;

import gov.gdlt.ssgly.taxcore.comm.servicelocator.SqlMapLocator;
import gov.gdlt.ssgly.taxcore.comm.config.ApplicationContext;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.common.util.PaginatedList;
import java.sql.SQLException;
import java.util.List;
import java.sql.Connection;
import java.util.Map;
import gov.gdlt.ssgly.taxcore.comm.servicelocator.JDBCLocator;

/**
 * <p>Title: 实现IDataAccessObject接口的基类</p>
 *
 * <p>Description: 广东地税税收管理员工作平台</p>
 *
 * <p>Copyright: Copyright (c) 2005 广东省地方税务局,广州市地方税务局</p>
 *
 * <p>Company: 广州地税</p>
 *
 * @author 郑毅晖
 * @version 1.0
 *
 * @version 1.1
 * modify: 加入了getCurrentConnection方法,以供直接使用JDBC访问数据库
 *
 * @author Lemon
 * @since 2005-10-15
 * modify: 自己管理数据库连接和事务而不使用iBatis来管理。
 */
public abstract class BaseDataAccessObject implements IDataAccessObject {
    protected SqlMapClient sqlMap;

    public BaseDataAccessObject() {
    }

    protected SqlMapClient getSqlMapClient() {
        this.sqlMap = SqlMapLocator.getInstance().getSqlMapClient();
        return this.sqlMap;
    }

    /**
     * 取得当前SqlMap使用的连接
     * @return Connection
     *   非空:当前的连接
     *   NULL:当前还没有可用连接
     * @throws SQLException
     */
    protected Connection getCurrentConnection() throws SQLException {
        this.getSqlMapClient(); //获取SqlMap对象
        return this.sqlMap.getCurrentConnection();
    }

    /**
     * 提供通用的查询一条记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @return int <br>
     *    ROWS_FOUND 执行成功,找到记录<br>
     *    ROWS_NOT_FOUND 执行成功,但找不到记录<br>
     *    RET_FAIL 执行失败
     */
    protected int select(String id, Object paramObj) {
        Object obj = null;

        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            obj = sqlMap.queryForObject(id, paramObj, this);

        } catch (SQLException e) {

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {

            }
        }

        if (obj == null) {
            return ApplicationContext.ROWS_NOT_FOUND;
        } else {
            return ApplicationContext.ROWS_FOUND;
        }
    }

    /**
     * 提供通用的查询一条记录的方法(另一个版本,直接返回查询到的结果)
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @return Object <br>
     *    非空 执行成功,找到记录<br>
     *    NULL 执行失败
     */
    protected Object select2(String id, Object paramObj) {
        Object obj = null;
        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            obj = sqlMap.queryForObject(id, paramObj);

        } catch (SQLException e) {
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {

            }
        }

        return obj;
    }

    /**
     * 提供通用的查询多条记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @return List <br>
     *    非空 执行成功,找到记录<br>
     *    NULL 执行失败
     */
    protected List selectAll(String id, Object paramObj) {
        List list = null;
        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            list = sqlMap.queryForList(id, paramObj);

        } catch (SQLException e) {
            return null;
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return list;
    }

    /**
     * 提供通用的查询多条特定范围记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @param skip:int 跳过的行数
     * @param max:int 读取的最大行数
     * @return List <br>
     *    非空 执行成功,找到记录<br>
     *    NULL 执行失败
     */
    protected List selectAll(String id, Object paramObj, int skip, int max) {
        List list = null;
        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            list = sqlMap.queryForList(id, paramObj, skip, max);

        } catch (SQLException e) {
            return null;
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return list;
    }

    /**
     * 提供通用的分页查询记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @param pageSize:int 每页的行数
     * @return PaginatedList <br>
     *    非空 执行成功,找到记录<br>
     *    NULL 执行失败
     */
    protected PaginatedList selectAllPaginated(String id, Object paramObj,
                                               int pageSize) {
        PaginatedList list = null;
        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            list = sqlMap.queryForPaginatedList(id, paramObj, pageSize);

        } catch (SQLException e) {
            return null;
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return list;
    }

    /**
     * 提供通用的删除记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @return int <br>
     *     >=0 被删除的记录数<br>
     *     RET_FAIL 执行失败
     */
    protected int delete(String id, Object paramObj) {
        int rows = 0;

        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            rows = sqlMap.delete(id, paramObj);
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
            }
            return ApplicationContext.RET_FAIL;

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return rows;
    }

    /**
     * 提供通用的删除记录的方法(另一个版本,使用本DAO的数据作为传入参数)
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @return int <br>
     *     >=0 被删除的记录数<br>
     *     RET_FAIL 执行失败
     */
    protected int delete(String id) {
        int rows = 0;

        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            rows = sqlMap.delete(id, this);
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
            }
            return ApplicationContext.RET_FAIL;

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return rows;
    }

    /**
     * 提供通用的更新记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @param paramObj:Object 传入的参数对象
     * @return int <br>
     *     >=0 被更新的记录数<br>
     *     RET_FAIL 执行失败
     */
    protected int update(String id, Object paramObj) {
        int rows = 0;

        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            rows = sqlMap.update(id, paramObj);
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
            }
            return ApplicationContext.RET_FAIL;

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }


        return rows;
    }

    /**
     * 提供通用的更新记录的方法(另一个版本,使用本DAO的数据作为传入参数)
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @return int <br>
     *     >=0 被更新的记录数<br>
     *     RET_FAIL 执行失败
     */
    protected int update(String id) {
        int rows = 0;
        this.getSqlMapClient(); //获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            rows = sqlMap.update(id, this);
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
            }
            return ApplicationContext.RET_FAIL;

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return rows;
    }

    /**
     * 提供通用的保存一条记录的方法
     * @param id:String SqlMap配置文件的SQL语句ID值
     * @return Object
     *     RET_FAIL:Integer 执行失败
     *     非RET_FAIL:Object 保存成功
     */
    protected Object save(String id) {
        Object generatedKey = null;
        this.getSqlMapClient(); //用父类方法获取SqlMap对象

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            generatedKey = sqlMap.insert(id, this);
            connection.commit();
        } catch (SQLException e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
            }
            return new Integer(ApplicationContext.RET_FAIL);

        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }

        return generatedKey;
    }

    protected Map selectAll(String id, Object paramObj, String key) {
        Map map = null;
        this.getSqlMapClient();

        Connection connection = null;
        try {
            connection = JDBCLocator.getInstance().getJDBCConnection();
            sqlMap.setUserConnection(connection);

            map = sqlMap.queryForMap(id, paramObj, key);

        } catch (SQLException e) {
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {

            }
        }

        return map;
    }
}

⌨️ 快捷键说明

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