日期:2014-05-16  浏览次数:20386 次

数据库操作基类设计1
// DAO基础接口
public interface BaseDao<T>
...............

// 数据库操作基类
public abstract class BaseDaoImpl<T> extends SqlMapClientDaoSupport implements BaseDao<T>
{
    private String entitySimpleClassName;
    public BaseDaoImpl()
    {
        entitySimpleClassName = ((Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]).getSimpleName();
    }
    public String getSequenceID() // 从数据库获取唯一序列号
    {
        String statementName = entitySimpleClassName + ".getSequence";
        return (String)getSqlMapClientTemplate().queryForObject(statementName);
    }
    public String insert(T t) // 持久化一个domain到数据库中
    {
        String statementName = entitySimpleClassName + ".insert";
        return (String)getSqlMapClientTemplate().insert(statementName, t);
    }
    public void insert(final List<T> ts) // 批量持久化
    {
        final String statementName = entitySimpleClassName + ".insert";
        getSqlMapClientTemplate().execute(new SqlMapClientCallback()
        {
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (T t : ts)
                {
                    executor.insert(statementName, t);
                }
                executor.executeBatch();
                return null;
            }
        });
    }