ibatis批量添加数据

public int addUserRole(List<UserRole> userRole) {
  int re = 1;
  SqlMapClient sqlMapClient = null;
  try {
   sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
   sqlMapClient.getDataSource().getConnection().setAutoCommit(false);
   sqlMapClient.startTransaction();   //开启事务    必须写,否则可能会出现事务没有开启的错误
   sqlMapClient.startBatch();        //开始批处理
   for (UserRole reply : userRole) {
    sqlMapClient.insert("insertUserRole", reply);
   }
   sqlMapClient.executeBatch();
   sqlMapClient.commitTransaction();
  } catch (Exception e) {
   e.printStackTrace();
   re = 0;
  } finally {
   try {
    // 结束事务
    sqlMapClient.endTransaction();
   } catch (SQLException e) {
    e.getMessage();
   }
  }
  // new Util().batchInsert("addWorkTrack", workTrack);
  return re;
 }

posted on 2013-06-17 11:42  火玉  阅读(536)  评论(0编辑  收藏  举报