感慨:10w条数据仅仅需要几百毫秒,厉害呀addBatch
Long begin = new Date().getTime();
Connection conn = null;
try {
log.info("开始将生成的兑换码批量插入数据库");
conn = DriverManager.getConnection(jdbcProperties.getUrl(), jdbcProperties.getUsername(), jdbcProperties.getPassword());
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement(" ");
String prefix = "INSERT INTO t_exchange_code (exchange_id,exchange_code,STATUS,creat_time) VALUES";
StringBuffer suffix = new StringBuffer();
for(int i = 0;i<exchangeCodes.size();i++){
suffix.append("( "+exchangeCodes.get(i).getExchangeId()+",\""+exchangeCodes.get(i).getExchangeCode()+"\","+exchangeCodes.get(i).getStatus()+",\""+DateUtils.getTime()+"\"),");
}
String sql = prefix + suffix.substring(0, suffix.length() - 1);
pst.addBatch(sql);
pst.executeBatch();
conn.commit();
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Long end = new Date().getTime();
log.info("兑换码number={}插入数据库花费的时间为time={}",exchangeCodes.size(),(end - begin) / 1000 + " s");
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties (prefix = "spring.datasource.druid.master")
public class JdbcProperties
{
private String url;
private String username;
private String password;
}
|