欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC分批更新 博客分类: 实战技术 JDBC分批更新分批更新 

程序员文章站 2024-03-18 10:38:34
...
@Override
	public boolean batchUpdatePackageBuyPrizeJdbc(
			List<BetPlanPackageBuy> buyList) {
		if (buyList == null || buyList.size() <= 0) {
			return true;
		}

		String upd_sql = "update bet_plan_package_buy set pretax_prize=?,postax_prize=? where id = ?";
		PreparedStatement ps = null;
		try {
			ps = myBatisTemplateBetCart.getConnection().prepareStatement(upd_sql);
			int rows = 0;
			for (BetPlanPackageBuy bt : buyList) {
				rows = rows + 1;
				ps.setBigDecimal(1, bt.getPretaxPrize());
				ps.setBigDecimal(2, bt.getPostaxPrize());
				ps.setLong(3, bt.getId());
				ps.addBatch();
				if (rows % 1000 == 0) {
					ps.executeBatch();
				}
			}
			ps.executeBatch();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
				}
			}
			ps = null;
		}

		return true;
	}