Ibatis结合Spring的使用:大致按下面6步
1:首先根据表,建一个序列化的实体类
2:MVN:引入ibatis相关的jar包
3:主的ibatis的配置文件
4:每个SqlMap的配置文件
5:spring的application-context.xml加上主的配置ibatis的配置文件
6:事务处理的实现类
7: 批量操作:
public void batchAddStockNewProduct(final List<StockNewProduct> productList) {
execute(new SqlMapClientCallback<Object>() {
@Override
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {executor.startBatch(); // 一定要有,通知开始批量
int batch = 0;
for (StockNewProduct stockNewProduct : productList) {
executor.insert("StockNewProduct.add", stockNewProduct);
// 每10条批量提交一次。
if (batch == 10) {
executor.executeBatch();
batch = 0;
}
}
executor.executeBatch(); // 不足10条记录
return null;
}
});
}