SqlMapClientDaoSupport 匿名内部类已经实现SqlMapClientCallback接口形式完成数据批处理

以下是dao层继承SqlMapClientDaoSupport 并采用匿名内部类方式将数据按批保存代码

public class BusinessManageDaoImpl extends  SqlMapClientDaoSupport  implements  BusinessManageDao{

    @Override
    public void insertBatchBusinessData( List list)
            throws DaoException {
            final List param_list=list;
         getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                    int batch = 0;
                    executor.startBatch();// 开始批处理
                    for (BusinessManageDto dto : param_list) {
                        batch++;
                        executor.insert("inserBusinessData", dto);
                        //每100条提交一次
                        if (batch%100 == 0) {//Oracle 11g推荐的batch大小介于50-100之间
                            executor.executeBatch();
                            executor.startBatch();
                        }
                    }
                    executor.executeBatch();// 执行批处理
                    return null;
                }
            });
    } 
  

考虑到更新文件的时候,打包的时候容易忘记把内部类文件打包,以前文章关于内部类class文件更新问题已提到过类似问题。所以就想着改写匿名内部类。
一般匿名内部类要就是抽象类,或者就是接口,可知SqlMapClientCallback属于接口,那就采用实现接口方式,新建类:SqlMapClientCallbackImpl,

public class SqlMapClientCallbackImpl<Object>  implements SqlMapClientCallback<Object>{

    @Override
    public Object doInSqlMapClient(SqlMapExecutor arg0) throws SQLException {
        return null;
    }
    public Object doInSqlMapClient(SqlMapExecutor executor,List list,String method) throws SQLException {
        int batch = 0;
        executor.startBatch();// 开始批处理
        for (Object obj : list) {
            batch++;
            executor.insert(method, obj);
            //每100条提交一次
            if (batch%100 == 0) {//Oracle 11g推荐的batch大小介于50-100之间
                executor.executeBatch();
                executor.startBatch();
            }
        }
        executor.executeBatch();// 执行批处理
        return null;
    }
} 
  

而 BusinessManageDaoImpl 类代码改为如下:

public class BusinessManageDaoImpl extends  SqlMapClientDaoSupport  implements  BusinessManageDao{

    @Override
    public void insertBatchBusinessData( List list)
            throws DaoException, SQLException {
        SqlMapClientCallbackImpl SqlMap=new SqlMapClientCallbackImpl() ;
        SqlMapClientTemplate sqlMapClientTemplate=getSqlMapClientTemplate();
        SqlMapExecutor executor=sqlMapClientTemplate.getSqlMapClient().getSession();
        SqlMap.doInSqlMapClient(executor,list, "insertBusinessData");
    }
} 
  

注意匿名内部类时,参数SqlMapExecutor实际是来自BusinessManageDaoImpl类中,所以改为,这里需要注意
SqlMapExecutor executor=sqlMapClientTemplate.getSqlMapClient().getSession();
改为实现类的方式,当然也能成功保存数据,但是效率却比匿名内部类方式更慢。后面发现只要重写doInSqlMapClient方法,不需要重载实现效率更快代码如下:

--把参数当成成员变量
public SqlMapClientCallbackImpl(List list,String sqlmapid){
        this.list = list;
        this.sqlmapid = sqlmapid;
    }
    @Override
    public Integer doInSqlMapClient(SqlMapExecutor sqlmapexecutor)
            throws SQLException {
        Integer sucessCount = new Integer("0");//成功执行的条数
        if (list != null && list.size() > 0) {
            sqlmapexecutor.startBatch();// 开始批处理
            for (int i = 0; i < list.size(); i++) {
                T obj = list.get(i);
                sqlmapexecutor.insert(sqlmapid, obj);
                // 每100条提交一次
                if (i % batchNum == 0) {// Oracle 11g推荐的batch大小介于50-100之间
                    sucessCount = sqlmapexecutor.executeBatch()+sucessCount;
                    sqlmapexecutor.startBatch();
                }
            }
            sucessCount = sqlmapexecutor.executeBatch()+sucessCount;// 执行批处理
        }
        return sucessCount;
    }

BusinessManageDaoImpl 类代码改为如下:

    public void insertBatchBusinessData( final List list)
            throws DaoException, SQLException {
         getSqlMapClientTemplate().execute(new SqlMapClientCallbackImpl(list, "insertBusinessData"));         
    }

完美!!!!

你可能感兴趣的:(Java)