public void batchUpdate( final String statementName, final List list) {
try {
if (list != null ) {
this .getSqlMapClientTemplate().execute( new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
executor.startBatch();
for ( int i = 0, n = list.size(); i < n; i++) {
executor.update(statementName, list.get(i));
}
executor.executeBatch();
return null ;
}
});
}
} catch (Exception e) {
if ( log .isDebugEnabled()) {
e.printStackTrace();
log .debug( "batchUpdate error: id [" + statementName + "], parameterObject [" + list + "]. Cause: " + e.getMessage());
}
}
}
public void batchInsert( final String statementName, final List list) {
try {
if (list != null ) {
this .getSqlMapClientTemplate().execute( new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
executor.startBatch();
for ( int i = 0, n = list.size(); i < n; i++) {
executor.insert(statementName, list.get(i));
}
executor.executeBatch();
return null ;
}
});
}
} catch (Exception e) {
if ( log .isDebugEnabled()) {
e.printStackTrace();
log .debug( "batchInsert error: id [" + statementName + "], parameterObject [" + list + "]. Cause: " + e.getMessage());
}
}
}
public void batchDelete( final String statementName, final List list) {
try {
if (list != null ) {
this .getSqlMapClientTemplate().execute( new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
executor.startBatch();
for ( int i = 0, n = list.size(); i < n; i++) {
executor.delete(statementName, list.get(i));
}
executor.executeBatch();
return null ;
}
});
}
} catch (Exception e) {
if ( log .isDebugEnabled()) {
e.printStackTrace();
log .debug( "batchDelete error: id [" + statementName + "], parameterObject [" + list + "]. Cause: " + e.getMessage());
}
}
}
本文介绍了一种使用MyBatis框架进行批量更新、插入和删除的方法。通过SqlMapClientTemplate执行批量操作,提高了数据处理效率。文章详细展示了如何实现这些功能,并提供了异常处理逻辑。
263

被折叠的 条评论
为什么被折叠?



