Multi-threaded batch modification of data
@RequestMapping("Test")
public class UpdateByThread implements Runnable {
private int startId;
private int endId;
//批量修改
@Autowired
ExaminationErrorMapper examinationErrorMapper;
@Autowired
ExaminationErrorCustMapper examinationErrorCustMapper;
public UpdateByThread(int startId,int endId){
this.startId=startId;
this.endId=endId;
}
@Override
public void run() {
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:config/applicationContext.xml");
examinationErrorMapper = (ExaminationErrorMapper) ac.getBean("examinationErrorMapper");
examinationErrorCustMapper = (ExaminationErrorCustMapper) ac.getBean("examinationErrorCustMapper");
String threadName = Thread.currentThread().getName();
int begin=Integer.valueOf(threadName.substring(threadName.indexOf("-")+1,threadName.length()));
//先查出需要修改的数据(建议使用limit 一条一条处理)
ExaminationError ee = examinationErrorCustMapper.selectOne(startId,endId,begin,1);
ExaminationError record = new ExaminationError();
record.setId(ee.getId());
record.setErrorcontent(String.valueOf(ee.getUserid()));
examinationErrorMapper.updateByPrimaryKeySelective(record);
}
}
Implement the Runnable interface and write the run method according to the business logic (the above is a test, so...)
@Controller
@RequestMapping(value="/Test")
public class TestContorller {
private static Logger logger = LoggerFactory.getLogger(TestContorller.class);
@RequestMapping(value = "/UpBT", method = RequestMethod.GET)
@ResponseBody
Map<String, Object> UpBT(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
try{
Integer startId = Integer.valueOf(request.getParameter("startId"));
Integer endId = Integer.valueOf(request.getParameter("endId"));
for(int i=0;i<50;i++){
new Thread(new UpdateByThread(startId,endId), "scoreAPI-"+i).start();
}
result.put("message","调用批量接口成功");
result.put("code", "0000");
}catch(Exception e){
e.printStackTrace();
result.put("code","9999");
result.put("message","调用批量接口失败");
return result;
}
return result;
}
}