最近有些利用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或其他