运用多线程批量修改数据

这篇博客探讨了如何通过实现Runnable接口并编写run方法,来利用多线程技术进行大规模数据的批量修改。文章内容简洁,主要关注于实际操作和业务逻辑的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多线程批量修改数据

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

      }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值