ibatis應(yīng)對批量update
最近遇到需要批量update數(shù)據(jù)的問題,一開始用了一個for循環(huán)去update,數(shù)據(jù)量大的時候效率很低。原因是for循環(huán)每次update一條語句,都是一次連接過程。遇到大批數(shù)據(jù)更新的時候,效率就可想而知了。在google上找了一遍,發(fā)現(xiàn)ibatis里有對批量update的支持,挺好的東西。
代碼如下:
final List tempList = list;
try {
if (List tempList != null) {
template.execute(
new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws
SQLException {
executor.startBatch();
for (int i = 0, n = tempList.size(); i < n; i++) {
executor.update("test.batchupdate",(Map)tempList.get(i));
}
executor.executeBatch();
return null;
}
}
);
}
}
ibatis批量update的用法就是這么簡單,但是其工作的原理還沒搞懂,希望大家指點。
【編輯推薦】