---
title: "特殊用法"
author: "primitive123"
date: "2014-12-12T15:24:17Z"
tags: []
categories: ["非技术"]
slug: "untitled"
draft: false
lastmod: "2014-12-12T15:24:17Z"
id: 2165864
url: "https://primitive123.iteye.com/blog/2165864"
---

```java
public void batchInsert(Sheet sheet) throws SQLException {
		Map<String, Object> map = null;
		try {
                        // 开启手动事务，关闭自动事务
			xjCommiBillReportDao.getSqlMapClient().startTransaction();
                        // 每执行executeBatch方法前必须先 startBatch一下。
			xjCommiBillReportDao.getSqlMapClient().startBatch();
			for (int i = 1, len = sheet.getLastRowNum(); i < len; i++) {
				map = row2Map(sheet.getRow(i));
				if (map == null)
					return;
				xjCommiBillReportDao.insert(map);
				if (i % 5000 == 0) {
// 数据量达到5000条时，批量插入数据库。
xjCommiBillReportDao.getSqlMapClient().executeBatch();
// 批量插入数据库后，必须再次开启startBatch，不然下次执行的executerBatch会失效，不会做批量插入数据库事件
xjCommiBillReportDao.getSqlMapClient().startBatch();
				}
			}
		} finally {
			xjCommiBillReportDao.getSqlMapClient().executeBatch();
// 关闭手动事务，开启自动事务
			xjCommiBillReportDao.getSqlMapClient().endTransaction();
		}
	}
```