@Override public <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> pss) throws DataAccessException { LogOperate logOperate = parseOperate(sql); Assert.notNull(logOperate,"不匹配的操作类型,日志记录失败!"); switch (logOperate){ case DELETE: batchDeleteRecordLog(logOperate,sql,batchArgs,pss); break; case UPDATE: batchUpdateRecordLog(logOperate,sql,batchArgs,pss); break; case INSERT: batchInsertRecordLog(logOperate,sql,batchArgs,pss); break; default: break; } return super.batchUpdate(sql, batchArgs, batchSize, pss); }
/** * Exemple d'utilisation du ParameterizedPreparedStatementSetter * Son int�r�t et de pouvoir utiliser des objets typ�s directement * * Par contre, il renvoie un int[][] pour refleter les actions sur chaque batchSize */ public int[][] updateUserAgain(final int batchSize, final User... usersToUpdate) { ParameterizedPreparedStatementSetter<User> ppss = new ParameterizedPreparedStatementSetter<User>() { @Override public void setValues(final PreparedStatement ps, final User user) throws SQLException { ps.setString(1, user.getName()); ps.setString(2, user.getMail()); ps.setInt(3, user.getId()); } }; return jdbcTemplate.batchUpdate("update users set name = ?, email = ? where id = ?", Arrays.asList(usersToUpdate), batchSize, ppss); }
@Before public void setUp(){ logger.info("populating database"); ParameterizedPreparedStatementSetter<String> ppss = new ParameterizedPreparedStatementSetter<String>() { @Override public void setValues(PreparedStatement preparedStatement, String s) throws SQLException { preparedStatement.setString(1, s); } }; jdbcTemplate.batchUpdate("DELETE FROM test_data"); int batchSize = 10000; for(int i=0; i< 5; i++) { jdbcTemplate.batchUpdate("INSERT INTO test_data (entry) VALUES (?)", Stream.generate(Math::random).map(String::valueOf).limit(1000000).collect(Collectors.toList()), batchSize, ppss); } logger.info("test data populated"); }
@Override public <T> int[][] batchUpdate( String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> ppss) { if (this.logger.isDebugEnabled()) { this.logger.debug("Executing SQL batch update [" + sql + "] with a batch size of " + batchSize); } try { return execute(sql, new ParameterizedBatchingPreparedStatementCallback<>(ppss, batchSize, batchArgs)); } finally { if (ppss instanceof ParameterDisposer) { ((ParameterDisposer) ppss).cleanupParameters(); } } }
private void doInPreparedStatement(int batchSize, int batchArgSize) throws SQLException { @SuppressWarnings("unchecked") ParameterizedPreparedStatementSetter<String> ppss = mock(ParameterizedPreparedStatementSetter.class); // Create the arguments for the batch update ArrayList<String> batchArgs = new ArrayList<>(batchArgSize); for (int i = 0; i < batchArgSize; i++) { batchArgs.add(Integer.toString(i)); } ParameterizedBatchingPreparedStatementCallback<String> psc = new ParameterizedBatchingPreparedStatementCallback<>(ppss, batchSize, batchArgs); int[][] result = psc.doInPreparedStatement(this.ops); assertThat(result, matchesBatchedRowCounts(batchSize, batchArgSize)); verifyPreparedStatementCalls(batchArgSize, ppss); }
@Override public void batchInsertUpdate(List<StockModelInfo> dtos) { template.batchUpdate(BATCH_INSERT, dtos, 100, new ParameterizedPreparedStatementSetter<StockModelInfo>() { @Override public void setValues(PreparedStatement ps, StockModelInfo dto) throws SQLException { ps.setInt(1, dto.getMarket()); ps.setString(2, dto.getStockCode()); ps.setBoolean(3, dto.getIsDel()); Date updateDate = new Date(dto.getUpdateDate().getTime()); ps.setDate(4, updateDate); ps.setString(5, dto.getStockDescription()); } }); }
@Override public void batchInsertUpdate(List<UserUrlMassage> dtos) { template.batchUpdate(BATCH_INSERT, dtos, 10, new ParameterizedPreparedStatementSetter<UserUrlMassage>() { @Override public void setValues(PreparedStatement ps, UserUrlMassage dto) throws SQLException { ps.setString(1, dto.getBoardId()); ps.setString(2, dto.getUrlId()); Date updateDate = new Date(dto.getDate().getTime()); ps.setDate(3, updateDate); ps.setBoolean(4, dto.getIsDel()); ps.setInt(5, dto.getSection()); ps.setBoolean(6, dto.getFlag()); } }); }
/** * Constructor. * @param ppss Parameterized Prepared Statement Setter. * @param sendBatchSize Size of the batch that will be sent to the DB. * @param batchArgs Batch arguments. */ public ParameterizedBatchingPreparedStatementCallback( ParameterizedPreparedStatementSetter<T> ppss, int sendBatchSize, Collection<T> batchArgs) { this.ppss = ppss; this.sendBatchSize = sendBatchSize < batchArgs.size() ? sendBatchSize : batchArgs.size(); this.batchArgs = new ArrayList<>(batchArgs); }
private void verifyPreparedStatementCalls(int batchArgSize, ParameterizedPreparedStatementSetter<String> ppss) throws SQLException { for (int i = 0; i < batchArgSize; i++) { verify(ppss).setValues(this.ops, Integer.toString(i)); verify(this.ops, times(batchArgSize)).executeUpdate(); } }
public <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> pss) throws DataAccessException { return delegate.batchUpdate(sql, batchArgs, batchSize, pss); }
@Override public <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> pss) throws DataAccessException { return super.batchUpdate(sql, batchArgs, batchSize, pss); }
private void batchUpdateRecordLog(LogOperate operate,String sql, Collection batchArgs, ParameterizedPreparedStatementSetter pss){ }
private void batchInsertRecordLog(LogOperate operate,String sql, Collection batchArgs, ParameterizedPreparedStatementSetter pss){ }
private void batchDeleteRecordLog(LogOperate operate,String sql, Collection batchArgs, ParameterizedPreparedStatementSetter pss){ }