ibatis應(yīng)對(duì)批量update
最近遇到需要批量update數(shù)據(jù)的問(wèn)題,一開(kāi)始用了一個(gè)for循環(huán)去update,數(shù)據(jù)量大的時(shí)候效率很低。原因是for循環(huán)每次update一條語(yǔ)句,都是一次連接過(guò)程。遇到大批數(shù)據(jù)更新的時(shí)候,效率就可想而知了。在google上找了一遍,發(fā)現(xiàn)ibatis里有對(duì)批量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的用法就是這么簡(jiǎn)單,但是其工作的原理還沒(méi)搞懂,希望大家指點(diǎn)。
【編輯推薦】