zoukankan      html  css  js  c++  java
  • JdbcTemplate 批量

    JDBC 批量处理

    public void insertBatch(final List<Customer> customers){
            
      String sql = "INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
                
      getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Customer customer = customers.get(i);
            ps.setLong(1, customer.getCustId());
            ps.setString(2, customer.getName());
            ps.setInt(3, customer.getAge() );
        }
                
        @Override
        public int getBatchSize() {
            return customers.size();
        }
      });
    }

    注意:url    jdbc:mysql://localhost:3306/yiibaijava  注意驱动url 的编写

    实例

    List<Object[]> batchargs = new ArrayList<>();
    String sql = "INSERT INTO tableName (commodityId, commodityName, commodityURL, commodityPrice, commodityPromo, commodityCommentCount, cityCode, shopType, shopTrade, status, shopId, shopName, updateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; Object[] das = {commodityId, commodityName, commodityURL, commodityPrice, commodityPromo, commodityCommentCount, cityCode, shopType, shopTrade, status, shopId, shopName, updateTime}; batchargs.add(das); if (batchargs.size() % 1000 == 0) { jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { for (int j = 0; j < 13; j++) { ps.setString(j + 1, batchargs.get(i)[j] == null ? "" : batchargs.get(i)[j].toString()); } } @Override public int getBatchSize() { return batchargs.size(); } }); batchargs.clear(); LOG.info("#######################-入库 :" + totalCount + "-#########################"); }
    故乡明
  • 相关阅读:
    Confluence 6 连接一个目录
    卸载 PrestaShop 1.7
    一“脚”到位-淋漓尽致的自动化部署
    从细节处谈Android冷启动优化
    视觉设计师的进化
    网易对象存储NOS图床神器
    移动端互动直播(入门篇)
    SpringBoot入门(五)——自定义配置
    SpringBoot入门(四)——自动配置
    SpringBoot入门(三)——入口类解析
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14954117.html
Copyright © 2011-2022 走看看