複数のスレッドを使用して、データをバッチで変更します

データのマルチスレッドバッチ変更

@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);


    }

}

Runnableインターフェースを実装し、ビジネスロジックに従ってrunメソッドを記述します(上記はテストなので...)

@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;

      }
    }

おすすめ

転載: blog.csdn.net/qq_37980436/article/details/73487654