ibatis 批量处理

最近有些利用ibatis的批量处理的东西。

例如批量插入:

 

public int inserFSDLBatch(final List<Fsdl> list) throws Exception {

this.getSqlMapClientTemplate().execute( new SqlMapClientCallback(){

public Object doInSqlMapClient(SqlMapExecutor executor)

throws SQLException {

executor.startBatch();

                for (Fsdl fsdl : list) {               

                    executor.insert("insertFSDL", fsdl); 

                }            

                executor.executeBatch();           

                return null;

 

            }

});

return Constants.CODE_DAO_SUCCESS;

}

 

 

 

例如批量删除,在网上找了下现在写下来:

1 直接进行自己拼字符串进行传值(在sql中留个占位符一样)

 

 

在映射文件中,按如下方法配置:

<delete id="batchDelete" parameterClass="java.lang.String">
   delete from tablename where id in ($ids$)
</delete>

 

 

因为是用美元符号进行处理,ibatis直接放进去不进行处理,这样我们就可以按照“,”进行连接起来,

ibatis直接组装成sql语句就好了。

 

2  用iterate  

比如<delete id="deleteByIds" parameterClass="java.util.List">
  delete from tablename where
  <iterate conjunction="," open="id in (" close=")">
   #[]#
</iterate>
  </delete>



3 对ibatis中对于批量处理的

public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
// 开始批处理  
executor.startBatch();
int num = 1;
for (Object tObject : memberList) {
// 插入操作  
executor.update(statement, tObject);
num ++;
if(num %rows == 0)
executor.executeBatch();
}
// 执行批处理  
executor.executeBatch();
return Constants.CODE_DAO_SUCCESS;
}

---------rows 设置成500或其他

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值