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

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

public class BusinessManageDaoImpl extends  SqlMapClientDaoSupport  implements  BusinessManageDao{

    @Override
    public void insertBatchBusinessData( List<BusinessManageDto> list)
            throws DaoException {
            final List<BusinessManageDto> param_list=list;
         getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>() {
                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<Object> 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<Object> list)
            throws DaoException, SQLException {
        SqlMapClientCallbackImpl<Object> SqlMap=new SqlMapClientCallbackImpl<Object>() ;
        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<BusinessManageDto> list)
            throws DaoException, SQLException {
         getSqlMapClientTemplate().execute(new SqlMapClientCallbackImpl<BusinessManageDto>(list, "insertBusinessData"));         
    }

完美!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的中年程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值