公司让我优化个代码,大体是这样的,先从数据库里查询所有的用户信息,63461条记录封装成代码中的list;然后再遍历整个list,组装成我们想要的vo,进行入库或更新操作。
//部分是之前的代码  
/* 是我优化的代码 
我测试下来发现 入2000条  组装这2000条数据花近4分钟时间,入库或更新的话不到1s。关于组装方面不知道大家有没有更新的写法或意见! 
for (UserAccountModel user : list) {
long betMoney,bounsMoney,billMoney,tixian;
map.put("userid",user.getUserId()+"");
map.put("userid", user.getUserId()+"");
mapMonth.put("userid",user.getUserId()+"");
map.put("transactiontype", betType+","+zhuiType+","+fhemaiType+","+chemaiType);

betMoney=this.userStatisticsDAO.selectBetTransactionCount(map);
map.put("transactiontype",tuiType);
betMoney-=this.userStatisticsDAO.selectTransactionCount(map);;
map.put("transactiontype",ozjType+","+hzjType);
bounsMoney=this.userStatisticsDAO.selectBetTransactionCount(map);
map.put("transactiontype",chongzhiType);
billMoney=this.userStatisticsDAO.selectTransactionCount(map);
map.put("transactiontype",jtxType+","+btxType);
tixian=this.userStatisticsDAO.selectBetTransactionCount(map);
month=this.getUserStatisticsDAO().selectDaylyForMonth(mapMonth);
allMonth=this.getUserStatisticsDAO().selectAllMonth(map);
betMoney=betMoney+month.getBetAccount()+allMonth.getBetAccount();
bounsMoney=bounsMoney+month.getBounsAccount()+allMonth.getBounsAccount();
billMoney=billMoney+month.getPrepaidPhoneAccount()+allMonth.getPrepaidPhoneAccount();
tixian=tixian+month.getCarryAccount()+allMonth.getCarryAccount();
//UserStatisticsModel statistics=this.getUserStatisticsDAO().findUserStatisticsByUid(user.getUserId()+"");
// if(statistics==null)
// statistics=new UserStatisticsModel();
// statistics.setBetAccount(betMoney);
// statistics.setBounsAccount(bounsMoney);
// statistics.setPrepaidPhoneAccount(billMoney);
// statistics.setCarryAccount(tixian);
// if(statistics.getUserId()==null){
// statistics.setUserId(user.getUserId()+"");
// this.getUserStatisticsDAO().addUserStatistics(statistics);
// }else{
// this.getUserStatisticsDAO().updateUserStatisticsByUid(statistics);
// }
       /*优化代码*/
statistics=new UserStatisticsModel();
statistics.setUserId(user.getUserId()+"");
statistics.setBetAccount(betMoney);
statistics.setBounsAccount(bounsMoney);
statistics.setPrepaidPhoneAccount(billMoney);
statistics.setCarryAccount(tixian);
statList.add(statistics);
count++;
if (count % 2000 == 0) {
System.out.println("用户账号统计--及时统计,组装1000结束" + new Timestamp(new Date().getTime()));
this.getUserStatisticsDAO().addAndUpdateUserStatistics(statList);
statList.clear();
System.out.println("statList的大小为:" + statList.size());
break;
}
}