Hibernate 中用JDBC批量新增记录:

 public int insertBatch(final List list){
    	return (Integer) getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				if(null==list || 0==list.size()){
					return 1;
				}
				
				Connection conn = session.connection();
				conn.setAutoCommit(false);
				PreparedStatement ps = null;
				String sql="INSERT INTO GoodsValue"+
					"(iinStorageDetailId,ddailyDate,fpresentPrice,icurrencyId," +
					"dcreateDate,icreateUser,istatus,smemo)"+
                    " VALUES(?,?,?,?,?,?,?,?)";
				ps = conn.prepareStatement(sql);
				int size = list.size();
				for(int i =0 ;i<size;i++){
					InStorageDetail isd = (InStorageDetail) list.get(i);
					modelToData(ps, isd);
					
					ps.addBatch();
					
					if(i%20==0){
						 ps.executeBatch();//每20条执行物理插入一次,返回int[]
						conn.commit();
					}
				}
				 ps.executeBatch();//余下不够20的插入,返回int[]
				conn.commit();
				conn.setAutoCommit(true);
				return 0;
			}
posted @ 2009-11-27 18:53  现役程序员  阅读(767)  评论(0编辑  收藏  举报