DBOperation的缺省实现

/*
 * 文件名:DBOperationDefault.java
 * 版权:Copyright 2008-2009 Huawei Tech.Co.Ltd.All Rights Reserved.
 * 描述:DBOperation的缺省实现。
 * 修改人:g00106664
 * 修改时间:2009-4-27
 * 修改内容:新增
 */
package com.huawei.widget.commons.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.dao.IncorrectResultSizeDataAccessException;

import com.huawei.widget.commons.dao.DBException;
import com.huawei.widget.commons.dao.DBOperation;
import com.huawei.widget.commons.dao.RowMapper;
import com.huawei.widget.commons.dao.SqlParameterSource;
import com.huawei.widget.commons.dao.das.DBProviderDAS;
import com.huawei.widget.commons.dao.spi.DBProvider;

/**
 * DBOperation的缺省实现。
 *
 * @author g00106664
 * @version C02 2009-4-27
 * @since OpenEye WIDGET_SRV V100R001C02
 */
@SuppressWarnings("unchecked")
public class DBOperationDefault implements DBOperation
{
    /**
     * 缺省为DBProviderDAS。
     */
    private DBProvider provider = new DBProviderDAS();

    /**
     * 此方法用于基本操作数据。
     *
     * @return the provider (type DBProvider) of this DBOperationDefault object.
     */
    public DBProvider getProvider()
    {
        return provider;
    }

    /**
     * 此方法用于设置基本操作。
     *
     * @param provider
     *            the provider of this DBOperationDefault object.
     */
    public void setProvider(DBProvider provider)
    {
        this.provider = provider;
    }

    /**
     * 此方法根据SQL查询并返回int类型。
     *
     * @param sql
     *            SQL语句。
     * @param args
     *            SQL参数,SqlParameterSource类型。
     * @return the int
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryForInt(String,
     *      SqlParameterSource...)
     */
    public int queryForInt(String sql, SqlParameterSource... args)
            throws DBException
    {
        return queryForObject(sql, Integer.class, args);
    }

    /**
     * Query and return long type
     *
     * @param sql
     *            SQL语句。
     * @param args
     *            参数
     * @return the long
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryForLong(String,
     *      SqlParameterSource...)
     */
    public long queryForLong(String sql, SqlParameterSource... args)
            throws DBException
    {
        return queryForObject(sql, Long.class, args);
    }

    /**
     * 此方法用于查询并返回基本类型。
     *
     * @param sql
     *            sql语句。
     * @param requiredType
     *            the required type
     * @param args
     *            the args
     * @return the t
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryForObject(String, Class<T>,
     *      SqlParameterSource...)
     */
    public <T> T queryForObject(String sql, Class<T> requiredType,
            SqlParameterSource... args) throws DBException
    {
        List list = query(sql, getSingleColumnRowMapper(requiredType), args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return (T) list.iterator().next();
    }

    /**
     * 此方法用于查询并返回实体对象。
     *
     * @param sql
     *            the sql
     * @param rm
     *            the rm
     * @param args
     *            the args
     * @return the t
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryForObject(String, RowMapper<T>,
     *      SqlParameterSource...)
     */
    public <T> T queryForObject(String sql, RowMapper<T> rm,
            SqlParameterSource... args) throws DBException
    {
        List list = query(sql, rm, args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return (T) list.iterator().next();
    }

    /**
     * 此方法用于查询并返回列表。
     *
     * @param sql
     *            the sql
     * @param rm
     *            rowMaper对象,以处理resultset的返回结果。
     * @param args
     *            the args
     * @return the list< t>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#query(String, RowMapper<T>,
     *      SqlParameterSource...)
     */
    public <T> List<T> query(String sql, RowMapper<T> rm,
            SqlParameterSource... args) throws DBException
    {
        return (List<T>) provider.query(sql, false,
                new RowMapperResultSetExtractor(rm), NOT_PAGE, NOT_PAGE, args);
    }

    /**
     * 此方法用于查询并返回列表。
     *
     * @param sql
     *            the sql
     * @param rm
     *            rowMaper对象,以处理resultset的返回结果。
     * @param args
     *            the args
     * @param pageNo
     *            the page no
     * @param pageSize
     *            the page size
     * @return the list< t>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#query(String, RowMapper<T>, int,
     *      int, SqlParameterSource...)
     */
    public <T> List<T> query(String sql, RowMapper<T> rm, int pageNo,
            int pageSize, SqlParameterSource... args) throws DBException
    {
        return (List<T>) provider.query(sql, false,
                new RowMapperResultSetExtractor(rm), pageNo, pageSize, args);
    }

    /**
     * 此方法用于查询并返回MAP对象,与返回实体对象对应。
     *
     * @param sql
     *            sql语句
     * @param args
     *            the args
     * @return the map< string, object>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryForMap(String,
     *      SqlParameterSource...)
     */
    public Map<String, Object> queryForMap(String sql,
            SqlParameterSource... args) throws DBException
    {
        List<Map> list = query(sql, getColumnMapRowMapper(), args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return list.iterator().next();
    }

    /**
     * 此方法通过SQL更新数据库并返回受影响的记录数。
     *
     * @param sql
     *            SQL语句。
     * @param args
     *            the args
     * @return the int
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#update(String,
     *      SqlParameterSource...)
     */
    public int update(String sql, SqlParameterSource... args)
            throws DBException
    {
        return provider.update(sql, false, args);
    }

    /**
     * 此方法用于批量更新数据库。
     *
     * @param sql
     *            SQL语句。
     * @param batchArgs
     *            待更新MAP数组参数。
     * @return the int[]
     * @see com.huawei.widget.db.DBOperation#batchUpdate(String,
     *      SqlParameterSource...)
     */
    public int batchUpdate(String sql, SqlParameterSource... batchArgs)
    {
        return provider.update(sql, false, batchArgs);
    }

    /**
     * Query by name for int.
     *
     * @param namingQuery
     *            the name query
     * @param args
     *            the args
     * @return the int
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryByNamingForInt(String,
     *      SqlParameterSource...)
     */
    public int queryByNamingForInt(String namingQuery,
            SqlParameterSource... args) throws DBException
    {
        return queryByNamingForObject(namingQuery, Integer.class, args);
    }

    /**
     * 此方法根据SQL查询并返回int类型。
     *
     * @param namingQuery
     *            命名查询,在配置文件中设置。
     * @param args
     *            SQL语句。
     * @return the long
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryByNamingForLong(String,
     *      SqlParameterSource...)
     */
    public long queryByNamingForLong(String namingQuery,
            SqlParameterSource... args) throws DBException
    {
        return queryByNamingForObject(namingQuery, Long.class, args);
    }

    /**
     * 此方法用于查询并返回基本类型。
     *
     * @param namingQuery
     *            命名查询,在配置文件中配置。
     * @param requiredType
     *            the required type
     * @param args
     *            the args
     * @return the t
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryByNamingForObject(String,
     *      Class<T>, SqlParameterSource...)
     */
    public <T> T queryByNamingForObject(String namingQuery,
            Class<T> requiredType, SqlParameterSource... args)
            throws DBException
    {
        List list = queryByNaming(namingQuery,
                getSingleColumnRowMapper(requiredType), args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return (T) list.iterator().next();
    }

    /**
     * 此方法用于查询并返回基本类型。
     *
     * @param namingQuery
     *            命名查询,在配置文件中配置。
     * @param rm
     *            the rm
     * @param args
     *            the args
     * @return the t
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryByNamingForObject(String,
     *      RowMapper<T>, SqlParameterSource...)
     */
    public <T> T queryByNamingForObject(String namingQuery, RowMapper<T> rm,
            SqlParameterSource... args) throws DBException
    {
        List list = queryByNaming(namingQuery, rm, args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return (T) list.iterator().next();
    }

    /**
     * 此方法用于查询并返回实体列表。
     *
     * @param namingQuery
     *            命名查询。
     * @param rm
     *            the rm
     * @param args
     *            the args
     * @return the list< t>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#queryByNaming(String, RowMapper<T>,
     *      SqlParameterSource...)
     */
    public <T> List<T> queryByNaming(String namingQuery, RowMapper<T> rm,
            SqlParameterSource... args) throws DBException
    {
        return (List<T>) provider.query(namingQuery, true,
                new RowMapperResultSetExtractor(rm), NOT_PAGE, NOT_PAGE, args);
    }

    /**
     * 此方法用于查询并返回实体列表,支持分页。
     *
     * @param namingQuery
     *            命名查询。
     * @param rm
     *            the rm
     * @param args
     *            the args
     * @param pageNo
     *            the page no
     * @param pageSize
     *            the page size
     * @return the list< t>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see #query(String, com.huawei.widget.db.RowMapper, int, int,
     *      com.huawei.widget.db.SqlParameterSource[])
     * @see com.huawei.widget.db.DBOperation#queryByNaming(String, RowMapper<T>,
     *      int, int, SqlParameterSource...)
     */
    public <T> List<T> queryByNaming(String namingQuery, RowMapper<T> rm,
            int pageNo, int pageSize, SqlParameterSource... args)
            throws DBException
    {
        return (List<T>) provider.query(namingQuery, true,
                new RowMapperResultSetExtractor(rm), pageNo, pageSize, args);
    }

    /**
     * 此方法用于查询并返回map对象,与返回实体对象相对应。
     *
     * @param namingQuery
     *            命名查询。
     * @param args
     *            the args
     * @return the map< string, object>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see #queryByNamingForObject(String, Class,
     *      com.huawei.widget.db.SqlParameterSource[])
     * @see com.huawei.widget.db.DBOperation#queryByNamingForMap(String,
     *      SqlParameterSource...)
     */
    public Map<String, Object> queryByNamingForMap(String namingQuery,
            SqlParameterSource... args) throws DBException
    {
        List<Map<String, Object>> list = queryByNaming(namingQuery,
                getColumnMapRowMapper(), args);

        int size = (list != null ? list.size() : 0);
        if (size == 0 || size > 1)
        {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }

        return list.iterator().next();
    }

    /**
     * 此方法根据命名SQL执行更新语句。
     *
     * @param namingQuery
     *            命名SQL。
     * @param args
     *            the args
     * @return the int
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     * @see com.huawei.widget.db.DBOperation#updateByNaming(String,
     *      SqlParameterSource...)
     */
    public int updateByNaming(String namingQuery, SqlParameterSource... args)
            throws DBException
    {
        return provider.update(namingQuery, true, args);
    }

    /**
     * 此方法根据命名SQL执行批量更新语句。
     *
     * @param namingQuery
     *            命名SQL。
     * @param batchArgs
     *            the batch args
     * @return the int[]
     * @see com.huawei.widget.db.DBOperation#batchUpdateByNaming(String,
     *      SqlParameterSource...)
     */
    public int batchUpdateByNaming(String namingQuery,
            SqlParameterSource... batchArgs)
    {
        return provider.update(namingQuery, true, batchArgs);
    }

    /**
     * 此方法根据SQL查询并返回列表,列表中的每个对象为MAP,其主键与查询中的字段名相对应。
     *
     * @param sql
     *            the sql
     * @param args
     *            the args
     * @return the list<Map>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     *
     * @see com.huawei.widget.db.DBOperation#queryForList(String,
     *      SqlParameterSource...)
     */
    public List<Map> queryForList(String sql, SqlParameterSource... args)
    {
        return query(sql, getColumnMapRowMapper(), args);
    }

    /**
     * 此方法根据SQL查询并返回列表,列表中的每个对象为MAP,其主键与查询中的字段名相对应。
     *
     * @param namingQuery
     *            命名SQL。
     * @param args
     *            the args
     * @return the list<Map>
     * @throws com.huawei.widget.db.DBException
     *             the DB exception
     */
    public List<Map> queryByNamingForList(String namingQuery,
            SqlParameterSource... args)
    {
        return queryByNaming(namingQuery, getColumnMapRowMapper(), args);
    }

    /**
     * Create a new RowMapper for reading result objects from a single column.
     *
     * @param requiredType
     *            the type that each result object is expected to match
     * @return the RowMapper to use
     * @see SingleColumnRowMapper
     */
    protected RowMapper getSingleColumnRowMapper(Class requiredType)
    {
        return new SingleColumnRowMapper(requiredType);
    }

    /**
     * 获取行映射 columnMapRowMapper
     *
     * @return the columnMapRowMapper (type RowMapper) of this
     *         DBOperationDefault object.
     */
    protected RowMapper getColumnMapRowMapper()
    {
        return new ColumnMapRowMapper();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值