批处理 rewriteBatchedStatements=true

时间:2025-03-17 19:36:38
public class NotifyRecordDaoTest extends BaseTest { @Resource(name = "masterDataSource") private DataSource dataSource; @Test public void insert() throws Exception { Connection connection = dataSource.getConnection(); connection.setAutoCommit(false); String sql = "insert into notify_record(" + " partner_no," + " trade_no, loan_no, notify_times," + " limit_notify_times, notify_url, notify_type,notify_content," + " notify_status)" + " values(?,?,?,?,?,?,?,?,?) "; PreparedStatement statement = connection.prepareStatement(sql); for (int i = 0; i < 10000; i++) { statement.setString(1, "1"); statement.setString(2, i + ""); statement.setInt(3, 1); statement.setInt(4, 1); statement.setString(5, "1"); statement.setString(6, "1"); statement.setString(7, "1"); statement.setString(8, "1"); statement.setString(9, "1"); statement.addBatch(); } long start = System.currentTimeMillis(); statement.executeBatch(); connection.commit(); connection.close(); statement.close(); System.out.println(System.currentTimeMillis() - start); } @Test public void insertB() { List<NotifyRecordEntity> notifyRecordEntityList = Lists.newArrayList(); for (int i = 0; i < 10000; i++) { NotifyRecordEntity record = new NotifyRecordEntity(); record.setLastNotifyTime(new Date()); record.setPartnerNo("1"); record.setLimitNotifyTimes(1); record.setNotifyUrl("1"); record.setLoanNo("1"); record.setNotifyContent("1"); record.setTradeNo("" + i); record.setNotifyTimes(1); record.setNotifyType(EnumNotifyType.DAIFU); record.setNotifyStatus(EnumNotifyStatus.FAIL); notifyRecordEntityList.add(record); } long start = System.currentTimeMillis(); Map<String, Object> params = Maps.newHashMap(); params.put("notifyRecordEntityList", notifyRecordEntityList); DaoFactory.notifyRecordDao.insertSelectiveList(params); System.out.println(System.currentTimeMillis() - start); } @Resource SqlSessionFactory sqlSessionFactory; @Test public void insertC() { SqlSession sqlsession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); NotifyRecordDao notifyRecordDao = sqlsession.getMapper(NotifyRecordDao.class); int num = 0; for (int i = 0; i < 10000; i++) { NotifyRecordEntity record = new NotifyRecordEntity(); record.setLastNotifyTime(new Date()); record.setPartnerNo("1"); record.setLimitNotifyTimes(1); record.setNotifyUrl("1"); record.setLoanNo("1"); record.setNotifyContent("1"); record.setTradeNo("s" + i); record.setNotifyTimes(1); record.setNotifyType(EnumNotifyType.DAIFU); record.setNotifyStatus(EnumNotifyStatus.FAIL); notifyRecordDao.insert(record); num++; // if(num>=1000){ // (); // (); // num=0; // } } long start = System.currentTimeMillis(); sqlsession.commit(); sqlsession.clearCache(); sqlsession.close(); System.out.println(System.currentTimeMillis() - start); } }