hibernate批量上传

         在做项目(SSH)的批量上传时,发现对数据库操作过于频繁会导致数据库崩溃,后来在网上查询之后发现,
  
  处理方法有两种:1.定量的处理缓存:session().flush();session.clear(); 2.用jdbcAPI代替hibernateAPI;当然也要尽量避免对数据库不必要的频繁查询;
  
  先贴一下DAO层用jdbc做的批量上传代码:
   
public void addDeviceByEXCELLot(List<DeviceINFO> plist) {
	try { 
	    Connection conn=DriverManager.getConnection(url,user,password);
	    int count=0;
	    String sql = "insert into  DEVICEINFO(DEVICEPK,DEVICENUMBER,CREATOR,LASTMODIFIER,CREATDATE,LASTMODIFDATE)"+
	    " values(DEVICEPK.NEXTVAL,?,?,?,?,?)";
	    PreparedStatement pstmt= conn.prepareStatement(sql);
	        for(DeviceINFO p:plist){
	          count++;
	          conn.setAutoCommit(false);//设置数据手动提交,自己管理事务 
	          pstmt.setString(1, p.getDeviceNumber());
	          pstmt.setString(2, p.getCreator());
	          pstmt.setString(3, p.getLastmodifire());
	          pstmt.setDate(4,new java.sql.Date(p.getCreatDate().getTime()));
	          pstmt.setDate(5,new java.sql.Date(p.getLastmodifDate().getTime()));
	       //用PreparedStatement的批量处理
	            pstmt.addBatch();
	            if(count==plist.size()){
	              pstmt.executeBatch();//执行批处理   
	              conn.commit();//提交   
	              conn.close();//关闭数据库   
	            }
	           
	        }
	          pstmt.close();   
	          conn.close();   
	          } catch (Exception e) {   
	          e.printStackTrace();   
	          }
	  }

  下面代码是action方法中对省市县的处理:

 provinceList=downloadservice.findallProvice();
		 //用来存储市Map,第一个Long存储省的ID,Map<String,Long>中String存储市的名称,Long存储市的ID
		 Map<Long,List<Map<String,Long>>> citymap =new HashMap<Long,List<Map<String,Long>>>();
		 //用来存储县Map,第一个Long存储市的ID,Map<String,Long>中String存储县的名称,Long存储县的ID
		 Map<Long,List<Map<String,Long>>> counrtymap =new HashMap<Long,List<Map<String,Long>>>();
		 if(provinceList!=null&&provinceList.size()>0){
			 for(Province p:provinceList){
				 //存储每个省对应的管属市集合
				 List<Map<String,Long>> ciList = new ArrayList<Map<String,Long>>();
				 Set<City> citySet = p.getCityset();
				 if(citySet!=null&&citySet.size()>0){
					 for(City c:citySet){
						 //用来存储每个市的对象Map
						 Map<String,Long> cmap = new HashMap<String,Long>();
						 cmap.put(c.getCityName(), c.getCityNumber());
						 ciList.add(cmap);
						 //存储每个市对应的管属县集合
						 List<Map<String,Long>> coList = new ArrayList<Map<String,Long>>();
						 Set<Country> countrySet =c.getCountryset();
						 if(countrySet!=null&&countrySet.size()>0){
							 for(Country co:countrySet){
								 //用来存储每个县的对象Map
								 Map<String,Long> comap = new HashMap<String,Long>();
								 comap.put(co.getCountryName(), co.getCountryNumber());
								 coList.add(comap);
							 }
						 }
						 counrtymap.put(c.getCityNumber(), coList);
					 }
				 }
				 citymap.put(p.getPnumber(), ciList);
			 }
		 }

	if(citymap!=null&&citymap.get(Pnum)!=null){
		        				//获取本省所管属的所有市集合
		        				List<Map<String,Long>> cityMapList = citymap.get(Pnum);
		        				if(cityMapList!=null&&cityMapList.size()>0){
		        					for(Map<String,Long> cmap:cityMapList){
		        						if(cmap.get(cellValue)!=null){//说明市存在
		        							Cname=cellValue;
		        							Cnum=cmap.get(cellValue);
		        							count++;
		        							break;
		        						}
		        					}
		        				}
		        			}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值