hutools之批量更新
public class HutoolTest { private static DataSource dataSource = DSFactory.get(); //读取默认路径下的配置文件,数据库连接以及线程池的配置 private static SqlRunner sqlRunner = SqlRunner.create(dataSource); public static String count = "select count(1) from company_with_industry where id > 5"; public static String currentPage = "select max(id) from company_with_industry where id in (select id from (select id from company_with_industry where id>? order by id limit 10000) as ids)"; public static String select = "select * from company_with_industry where id>? order by id limit 10000"; public static String update = "update company_with_industry set industry = ? where id = ?"; private static Queue<Long> queue = new ConcurrentLinkedDeque<>(); static Map<String, String> map = new HashMap<>(); static ExecutorService threadPool = Executors.newFixedThreadPool(10); @Test public void test() { initQueue(); readFile(); updateIndustry(); } public static void initQueue() { Entity entity = Entity.create("company_with_industry"); long count = 0; long totalPage = 0; long currentId = 5; try { count = sqlRunner.count(entity); totalPage = count % 10000 == 0 ? count / 10000 : count / 10000 + 1; queue.add(currentId); for (int i = 1; i < totalPage; i++) { long id = sqlRunner.queryNumber(currentPage, currentId).longValue(); currentId = id; //System.out.println(currentId); queue.add(currentId); } System.out.println(queue); } catch (SQLException e) { e.printStackTrace(); } System.out.println(count); } public static void readFile() { try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("行业关键字.txt")), "UTF-8")); String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); String[] ss = line.split("--"); for (String keyword : ss[1].split(",")) { map.put(keyword, ss[0]); } } System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } public static void updateIndustry() { while (true) { if(queue.size()>0){ long currentPage = queue.poll(); if (currentPage != 0) { threadPool.execute(() -> { try { List<Entity> list = sqlRunner.query(select, currentPage); Object[][] objects = new Object[1000][2]; int count = 0; for (Entity entity : list) { String entName = entity.getStr("ent_name"); if (!entName.contains("?")) { for (String keyword : map.keySet()) { if (entName.contains(keyword)) { objects[count][0] = map.get(keyword); objects[count][1] = entity.getLong("id"); count++; break; } } } if(count==1000){ sqlRunner.executeBatch(update,objects); count = 0; objects = new Object[1000][2]; System.out.println(Thread.currentThread().getName()+"\tgank了1000条数据\t"+entity.getLong("id")); } } if(objects.length>0){ sqlRunner.executeBatch(update,objects); System.out.println(Thread.currentThread().getName()+"\tgank了1000条数据\t"+list.get(list.size()-1).getLong("id")); } } catch (Exception e) { e.printStackTrace(); System.out.println(currentPage); } }); } else { System.out.println("没任务了,休息5秒钟!"); try { Thread.currentThread().sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }