使用hibernate的批量更新提高效率

 

 

/**
	 * 发布成绩
	 * @param scoreList
	 * @param examList
	 */
	public void publishScore(List scoreList,
			List examList) {
		nationScoreinfoDao.getSession().doWork(new PublishScoreWork(scoreList,examList));
	}
	
	private static class PublishScoreWork implements Work{
		List scoList ;
		List exList;
		
		
		public PublishScoreWork(List scoreList,List examList){
			scoList = scoreList;
			exList = examList;
		}
		
		@Override
		public void execute(Connection connection) throws SQLException {
			PreparedStatement pstmt=connection.prepareStatement("update nation_scoreinfo ns set ns.score_status = '"+EScoreStatus.releaseSuccess.toString()+"' where ns.uuid = ?");
			pstmt.clearBatch();
			for(NationScoreinfo r : scoList){
				pstmt.setString(1, r.getUuid());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			pstmt=connection.prepareStatement("update nation_examineedetail ne set ne.status = '"+EExamStatus.scoreQuery.toString()+"' where ne.uuid = ?");
			for(NationExamineedetail ne : exList){
				pstmt.setString(1, ne.getUuid());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			System.out.println("批量发布完成");
		}
		
	}

 必须注意 pstmt.addBatch();   使用

你可能感兴趣的:(java)